I have the following data frame in R
DF1<-data.frame("S"=c("A", "B", "C"))
namelist<-c("A", "B", "C")
DF1[c("A", "B", "C")]<-c(1,2,3)
DF1$B<-DF1$B*2
I have created a new column Sum as follows
DF1$Sum<-0
The resulting dataframe is as follows
S A B C Sum
A 1 2 1 0
B 2 4 2 0
C 3 6 3 0
I want the values in Sum to be filled in such a way that the values against row A of Sum will be equal to the sum of the entries in column A, and similarly for B and C. The result should be as follows
S A B C Sum
1 A 1 2 1 6
2 B 2 4 2 10
3 C 3 6 3 6
I have tried the following code
for( i in 1:nrow(DF1)){
for( j in namelist){
DF1$Sum[DF1$S[j][i]]=sum(DF1[,j])
}}
I am not getting the desired result. I am getting the following output. I request someone to help me here.
S A B C Sum
1 A 1 2 1 0
2 B 2 4 2 0
3 C 3 6 3 0