Adding two rows via addition

I have a data frame with a lot of rows. I now would like to add the rows 2 and 5 so that row 5 remains with the new values:

x y z
1 1 1 0
2 1 1 0
3 1 1 2
4 2 0 1
5 2 1 1
6 2 0 1

OUTPUT:

x y z
1 1 1 0
2 1 1 0
3 1 1 2
4 2 0 1
5 3 2 1
6 2 0 1

Unfrotunately, when I try to exert df[1,]<-df[1,]+df[2,] in my dataframe, the error term: Error in FUN(left, right) : non-numeric argument to binary operator emerges. I would appreciate some help.

Your example works for me.

 DF <- data.frame(x=c(1,1,1,2,2,2),
                  y=c(1,1,1,0,1,0),
                  z=c(0,0,2,1,1,1))
DF
  x y z
1 1 1 0
2 1 1 0
3 1 1 2
4 2 0 1
5 2 1 1
6 2 0 1
DF[5,] <- DF[2,]+DF[5,]
DF
  x y z
1 1 1 0
2 1 1 0
3 1 1 2
4 2 0 1
5 3 2 1
6 2 0 1

What do you get when you run

str(df)

Are all of your columns numeric or is there a character column?

Hi, all columns are labelled "labelled" and "integer"

Please post the output of

dput(head(df,10))

Place a line with three back ticks just before and after the output, like this
```
Pasted output
```

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.