Say, I have a data frame as below, having three columns. I would like to create a fourth column whose value is the sum of the number of ones in the first two columns put together
An iterative version
col1 = c(1,1,1,0,0)
col2 = c(1,0,0,0,1)
col3 = c(0,1,1,1,0)
df = data.frame(col1,col2,col3)
df$col4 = 0;
for(i in 1:nrow(df))
{
print(i)
countone = 0
for(j in 1:2)
{
if(df[i,j] == 1)
{
countone = countone + 1;
}
}
df[i,4] = countone
}
Input
col1 col2 col3
1 1 1 0
2 1 0 1
3 1 0 1
4 0 0 1
5 0 1 0
Expected Output
> df
col1 col2 col3 col4
1 1 1 0 2
2 1 0 1 1
3 1 0 1 1
4 0 0 1 0
5 0 1 0 1