[英]Get sum of every n th column for each individual and create new data frame in r
搜索了類似的帖子后,我發布了我的問題。 每個站點我有幾年的月降雨量變量。 我需要計算多年來的月平均降雨量。 我給出了一個簡單的數據框如下。 我需要創建一個新的數據框架,其中包含每個站點的月平均值(12)。
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))
輸出應該是這樣的;
df<-data.frame(id = c("A","B","C","D"))
df[c("jan","feb","mar","apr","may","jun")]<-NA
例如,單元格A1應包含X2000_1和X2001_1的平均降雨量
我嘗試了我的代碼如下,但它不起作用可能是因為我使用數據框。 任何幫助將非常感激。
n = 6
unname(tapply(d, (seq_along(d)-1) %/% n, sum))
我的實際數據框的列名是
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")
您可以從列名稱中提取月份作為變量,並通過months變量將數據幀拆分為列表,並使用rowMeans()
函數計算每個子數據幀的行平均值:
# 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
您還可以通過對一個長數據集進行reshape
整形以及制表來實現:
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
這是使用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
要不就
cbind(d[1], (d[2:7] + d[8:13])/2)
假設,我們將第一列作為ID
並且所有列均勻分布。
我們可以將數據幀分成兩半並得到它們之間的平均值。
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
顯然,我們總是可以通過硬編碼列號來完成。
cbind(d[1], (d[2:7] + d[8:13])/2)
但是,上面提到的方法是一般化的,即使我們有超過13列,它也會工作。
據我所知,要獲取文件的簽出信息,您需要找出工作區,然后找到這些工作空間上所有待處理的更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.