I have a really simple problem but haven\'t been able to find a solution. I am hoping someone can help. I have a dataframe test3
:
test3 <- st
Here's one approach:
> transform(test3, C=rowSums(test3, na.rm=TRUE))
A B C
1 1 NA 1
2 2 NA 2
3 NA 3 3
4 4 NA 4
Consider the following data.frame test3
with an additional column AA
, you can use the operator [
to subet the columns you are interested in:
> set.seed(1) # adding a new column
> test3$AA <- rnorm(4, 10, 1)
> test3 # this is how test3 looks like
A B AA
1 1 NA 9.373546
2 2 NA 10.183643
3 NA 3 9.164371
4 4 NA 11.595281
> transform(test3, C=rowSums(test3[, c("A", "B")], na.rm=TRUE))
A B AA C
1 1 NA 9.373546 1
2 2 NA 10.183643 2
3 NA 3 9.164371 3
4 4 NA 11.595281 4