-1

I have asked this question a couple times without any help. I have since improved the code so I am hoping somebody has some ideas! I have a dataset full of 0's and 1's. I simply want to add the 10 columns together resulting in 1 column with 3835 rows. This is my code thus far:

# select for valid IDs
data = history[history$studyid %in% valid$studyid,]

sibling = data[,c('b16aa','b16ba','b16ca','b16da','b16ea','b16fa','b16ga','b16ha','b16ia','b16ja')]

# replace all NA values by 0
 sibling[is.na(sibling)] <- 0

# loop over all columns and count the number of 174
apply(sibling, 2, function(x) sum(x==174))

The problem is this code adds together all the rows, I want to add together all the columns so I would result with 1 column. This is the answer I am now getting which is wrong:

b16aa b16ba b16ca b16da b16ea b16fa b16ga b16ha b16ia b16ja 
   68    36    22    18     9     5     6     5     4     1 
Brianna Marie
  • 43
  • 1
  • 3
  • 2
    Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Maybe `apply(sibling, 1, function(x)sum(x==174))` helps. Please read `?apply` (read about the `MARGIN` argument). – sgibb May 10 '13 at 04:05
  • 4
    The reason you keep getting minimal help is that you keep posting non-reproducible examples! – mnel May 10 '13 at 04:06
  • How do you get the result you post here? You wrote your dataset consists of 0 and 1 - in that case your `apply` command should report just zeros, isn't it?! Somehow it's difficult to see what data you have and what you want to get as result. – Daniel Fischer May 10 '13 at 07:18

1 Answers1

0

In apply() you have the MARGIN set to 2, which is columns. Set the MARGIN argument to 1, so that your function, sum, will be applied across rows. This was mentioned by @sgibb.

If that doesn't work (can't reproduce example), you could try first converting the elements of the matrix to integers X2 <- apply(sibling, c(1,2), function(x) x==174), and then use rowSums to add up the columns in each row: Xsum <- rowSums(X2, na.rm=TRUE). With this setup you do not need to first change the NA's to 0's, as you can just handle the NA's with the na.rm argument in rowSums()

rbatt
  • 4,677
  • 4
  • 23
  • 41
  • This is the closest I have gotten! The answer is correct, except I get one long row when I want this to be a column. – Brianna Marie May 10 '13 at 20:29
  • use the t() function -- it'll turn a row into a column, or vice versas (t for transpose). That work? – rbatt May 10 '13 at 22:05