0

Hope all of you're fine. Here, I come to get a solution. I am trying to rearrange my datasheet. My data sheet in the following format

Family   Guild  XY  AB  CD
Fam1      I     25  0   7
Fam2      I     0   12  0
Fam2      F     0   0   5
Fam3      G    134  0  124

First, I want to make new data frame into following format

family Guild   XY   AB   CD  Total  Freq  
Fam1     I     25    0    7    32
Fam2     I     0    12    0    12
Fam2     F     0     0    5     5
Fam3     G    134    0  124    258
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

2

We can use rowSums

transform(
  df,
  Freq = rowSums(cbind(XY, AB, CD))
)

or (thank @Onyambu's comment)

transform(df,  Freq =XY+ AB+ CD)

or

df$Freq <- rowSums(df[-c(1:2)])

which gives

  Family Guild  XY AB  CD Freq
1   Fam1     I  25  0   7   32
2   Fam2     I   0 12   0   12
3   Fam2     F   0  0   5    5
4   Fam3     G 134  0 124  258
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81