You could simply do:
dat1[dat1==4] <- 0
rowSums(dat1)
If modifying the object is not acceptable just copy it first:
dat1Zero <- dat1
dat1[dat1Zero==4] <- 0
rowSums(dat1Zero)
Question
If I have these data (dat1)
` L1 L2 L3 L4 L5 L6
1 0 1 0 4 0
4 1 0 1 1 0
1 0 0 1 4 1
All the values in the matrix are 0,1,4 I need to sum every row ignoring number 4. The result like this
2
3
3
I have more than 900 rows.. I tried these but there is something missing
rowSums(subset(dat1,L1!=4)
rowSums(which[dat1!=4])
n4=dat1[dat1==4]<-0
Solution
You could simply do:
dat1[dat1==4] <- 0
rowSums(dat1)
If modifying the object is not acceptable just copy it first:
dat1Zero <- dat1
dat1[dat1Zero==4] <- 0
rowSums(dat1Zero)
OTHER TIPS
If all the values are in (0,1,4) then you just want to count the 1's, no??
apply(df,1,function(x)sum(x==1))
# [1] 2 3 3