Get sum of every n th column for each individual and create new data frame in r

100 views Asked by At

Having searched for similar posts, I am posting my question. I have monthly rainfall variables for several years for each site. I need to calculate monthly average rainfall over the years. I have given a simple data frame as follows. I need to create a new data frame that consists of monthly averages (12) for each site.

d<-structure(list(ID = structure(1:4, .Label = c("A", "B", "C", 
"D"), class = "factor"), X2000_1 = c(25L, 42L, 74L, 52L), X2000_2 = c(15L, 
15L, 51L, 12L), X2000_3 = c(14L, 21L, 25L, 41L), X2000_4 = c(74L, 
4L, 23L, 51L), X2000_5 = c(15L, 25L, 65L, 12L), X2000_6 = c(31L, 
23L, 15L, 25L), X2001_1 = c(52L, 54L, 18L, 63L), X2001_2 = c(85L, 
165L, 12L, 12L), X2001_3 = c(25L, 36L, 20L, 14L), X2001_4 = c(1L, 
17L, 23L, 52L), X2001_5 = c(24L, 45L, 12L, 15L), X2001_6 = c(3L, 
23L, 45L, 52L)), .Names = c("ID", "X2000_1", "X2000_2", "X2000_3", 
"X2000_4", "X2000_5", "X2000_6", "X2001_1", "X2001_2", "X2001_3", 
"X2001_4", "X2001_5", "X2001_6"), class = "data.frame", row.names = c(NA, 
-4L))

The output should be like;

df<-data.frame(id = c("A","B","C","D"))
df[c("jan","feb","mar","apr","may","jun")]<-NA

for example the cell A1 should contains the average rainfall of X2000_1 and X2001_1

I tried my codes as below but it does not work may be because I am using data frame. Any help would be much appreciated.

n = 6
unname(tapply(d, (seq_along(d)-1) %/% n, sum))

The column names of my actual data frame are

c("est", "X1990_1", "X1990_2", "X1990_3", "X1990_4", "X1990_5", 
"X1990_6", "X1990_7", "X1990_8", "X1990_9", "X1990_10", "X1990_11", 
"X1990_12", "X1991_1", "X1991_2", "X1991_3", "X1991_4", "X1991_5", 
"X1991_6", "X1991_7", "X1991_8", "X1991_9", "X1991_10", "X1991_11", 
"X1991_12", "X1992_1", "X1992_2", "X1992_3", "X1992_4", "X1992_5", 
"X1992_6", "X1992_7", "X1992_8", "X1992_9", "X1992_10", "X1992_11", 
"X1992_12", "X1993_1", "X1993_2", "X1993_3", "X1993_4", "X1993_5", 
"X1993_6", "X1993_7", "X1993_8", "X1993_9", "X1993_10", "X1993_11", 
"X1993_12", "X1994_1", "X1994_2", "X1994_3", "X1994_4", "X1994_5", 
"X1994_6", "X1994_7", "X1994_8", "X1994_9", "X1994_10", "X1994_11", 
"X1994_12", "X1995_1", "X1995_2", "X1995_3", "X1995_4", "X1995_5", 
"X1995_6", "X1995_7", "X1995_8", "X1995_9", "X1995_10", "X1995_11", 
"X1995_12", "X1996_1", "X1996_2", "X1996_3", "X1996_4", "X1996_5", 
"X1996_6", "X1996_7", "X1996_8", "X1996_9", "X1996_10", "X1996_11", 
"X1996_12", "X1997_1", "X1997_2", "X1997_3", "X1997_4", "X1997_5", 
"X1997_6", "X1997_7", "X1997_8", "X1997_9", "X1997_10", "X1997_11", 
"X1997_12", "X1998_1", "X1998_2", "X1998_3", "X1998_4", "X1998_5", 
"X1998_6", "X1998_7", "X1998_8", "X1998_9", "X1998_10", "X1998_11", 
"X1998_12", "X1999_1", "X1999_2", "X1999_3", "X1999_4", "X1999_5", 
"X1999_6", "X1999_7", "X1999_8", "X1999_9", "X1999_10", "X1999_11", 
"X1999_12", "X2000_1", "X2000_2", "X2000_3", "X2000_4", "X2000_5", 
"X2000_6", "X2000_7", "X2000_8", "X2000_9", "X2000_10", "X2000_11", 
"X2000_12")
5

There are 5 answers

12
Psidom On BEST ANSWER

You can extract the months as variable from the column names, and split the data frame as a list by the months variable and for each sub data frame calculate the row average with the rowMeans() function:

# extract the months for each column
mon <- sub(".*_(\\d+)$", "\\1", names(d)[-1])

# split the data frame by columns and calculate the rowMeans
cbind.data.frame(d[1], lapply(split.default(d[-1], mon), rowMeans))

#  ID    1    2    3    4    5    6
#1  A 38.5 50.0 19.5 37.5 19.5 17.0
#2  B 48.0 90.0 28.5 10.5 35.0 23.0
#3  C 46.0 31.5 22.5 23.0 38.5 30.0
#4  D 57.5 12.0 27.5 51.5 13.5 38.5
0
thelatemail On

You could also do it with some reshape-ing to a long dataset, along with tabulation:

tmp <- reshape(d, idvar="ID", sep="_", direction="long", varying=-1)
xtabs(rowMeans(cbind(X2000,X2001)) ~ ID + time, data=tmp)
#   time
#ID     1    2    3    4    5    6
#  A 38.5 50.0 19.5 37.5 19.5 17.0
#  B 48.0 90.0 28.5 10.5 35.0 23.0
#  C 46.0 31.5 22.5 23.0 38.5 30.0
#  D 57.5 12.0 27.5 51.5 13.5 38.5
0
Ronak Shah On

Assuming , we have first columns as ID and rest all columns are equally distributed.

Can we just, divide the dataframe into two halves and get the mean between them.

cbind(d[1],(d[2:ceiling(ncol(d)/2)] + d[(ceiling(ncol(d)/2) + 1):ncol(d)])/2)


#   ID X2000_1 X2000_2 X2000_3 X2000_4 X2000_5 X2000_6
#1  A    38.5    50.0    19.5    37.5    19.5    17.0
#2  B    48.0    90.0    28.5    10.5    35.0    23.0
#3  C    46.0    31.5    22.5    23.0    38.5    30.0
#4  D    57.5    12.0    27.5    51.5    13.5    38.5

Obviously, we can always do by hard coding the column numbers.

cbind(d[1], (d[2:7] + d[8:13])/2)

However, the above mentioned approach is generalized one and it will work even if we have more than 13 columns.

1
nikhil sharma On

As far as I know, to get the Check-out information of a file you need to find out the workspaces and then find what are all pending changes on those work spaces.

2
akrun On

Here is an option using Reduce with +

cbind(d[1], Reduce(`+`, list(d[2:7], d[8:13]))/2)
#    ID X2000_1 X2000_2 X2000_3 X2000_4 X2000_5 X2000_6
#1  A    38.5    50.0    19.5    37.5    19.5    17.0
#2  B    48.0    90.0    28.5    10.5    35.0    23.0
#3  C    46.0    31.5    22.5    23.0    38.5    30.0
#4  D    57.5    12.0    27.5    51.5    13.5    38.5

Or just

cbind(d[1], (d[2:7] + d[8:13])/2)