I have a dataframe with 2 columns and I want to use a if/else condition when using the apply function to sum() the rows in each column - specifically, for all the rows where Col1 >= Col2 take the sum() of Col1 and store it in variable a, and for all the rows where Col1 < Col2 take the sum() of Col1 and store it in variable b.
For example
df<-data.frame(Col1=c(1,2,3,4,5),Col2=c(5,4,3,2,1)
df
Col1 Col2
1 5
2 4
3 3
4 2
5 1
There are three instances in which Col1 >= Col2, so in Col1 I take the sum() of 3+4+5, which is 12. There are two instances in which Col1 < Col2, so in Col1 I take the sum() of 1+2, which is 3. So
>a
12
>b
3
Aucun commentaire:
Enregistrer un commentaire