0

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
smci
  • 32,567
  • 20
  • 113
  • 146
IAMTubby
  • 1,627
  • 4
  • 28
  • 40

1 Answers1

1
df$col4 <- rowSums(df[,1:2])

That's called a row-sum. And there is no need for the initialization line: df$col4 = 0.

( This code works because we are given those entries can only be 0 or 1. Something that works on more general code would be rowSums(df[,1:2]==1), with ... na.rm = T if needed. )

smci
  • 32,567
  • 20
  • 113
  • 146