Re: [R] Adding two or more columns of a data frame for each row when NAs are present.

2011-11-22 Thread Ian Strang
I think, here is the solution. If NA is included in read.table list the row becomes a factor: $ Q21: Factor w/ 3 levels " 1"," 2"," NA": 1 2 3 2 2. This will not work with rowSums. If I put the missing value as a blank, then it is still read as NA but the whole row is considered as an integer an

Re: [R] Adding two or more columns of a data frame for each row when NAs are present.

2011-11-21 Thread Ian Strang
Hi, Thanks, your method does indeed work. Thank you. Last night, I worked out something similar and found out about rowMeans as well. Kind wishes, Ian yy <- read.table( header = T, sep=",", text = "Q20, Q21, Q22, Q23, Q24 0,1, 2,3,4 1,NA,2,3,4 2,1, 2,3,4 5,NA,3,NA,NA") yy Q20 Q21 Q22 Q23

Re: [R] Adding two or more columns of a data frame for each row when NAs are present.

2011-11-20 Thread David Winsemius
On Nov 20, 2011, at 3:38 PM, Ian Strang wrote: I am fairly new to R and would like help with the problem below. I am trying to sum and count several rows in the data frame yy below. All works well as in example 1. When I try to add the columns, with an NA in Q21, I get as NA as mySum. I

Re: [R] Adding two or more columns of a data frame for each row when NAs are present.

2011-11-20 Thread Dennis Murphy
Hi: Does this work for you? > yy Q20 Q21 Q22 Q23 Q24 1 0 1 2 3 4 2 1 NA 2 3 4 3 2 1 2 3 4 rowSums(yy, na.rm = TRUE) 1 2 3 10 10 12 # Use a subset of the variables in yy: selectVars <- paste('Q', c(20, 21, 24), sep = '') rowSums(yy[, selectVars], na.rm = TRUE) 1

[R] Adding two or more columns of a data frame for each row when NAs are present.

2011-11-20 Thread Ian Strang
I am fairly new to R and would like help with the problem below. I am trying to sum and count several rows in the data frame yy below. All works well as in example 1. When I try to add the columns, with an NA in Q21, I get as NA as mySum. I would like NA to be treated as O, or igored. I wrote