[英]Query and aggregate data based on conditions in R
我有一個數據框,如果type a
值等於1,我希望得到每年type b
的所有值的均值。
Year type value1 value2 value3 value4 value5
1 a 1 1 2 3 4
1 b 10 12 9 8 10
2 a 1 2 2 2 1
2 b 11 10 13 9 14
所以我的最終產品看起來像這樣:
Year type_b_values
1 11
2 12.5
這是Year1
的value1
和value2
的平均值,以及Year1
的value1
和5
的Year2
。 謝謝!
這是一種使用基本函數的方法。 我猜plyr或reshape也可能是有用的包,但我對它們不太熟悉:
dat <- read.table(text="Year type value1 value2 value3 value4 value5
1 a 1 1 2 3 4
1 b 10 12 9 8 10
2 a 1 2 2 2 1
2 b 11 10 13 9 14", header=TRUE)
dat_split <- split(dat, dat$Year) # split our data into a list by year
output <- sapply(dat_split, function(x) {
y <- x[x$type == "a", -c(1:2)] == 1 # which a in that year = 1
z <- x[x$type == "b", -c(1:2)][y] # grab the b values that a = 1
if (sum(y) == 0) { # eliminate if no a = 1
return(NA)
}
mean(z)
})
data.frame(Year = names(output), type_b_values = output)
## > data.frame(Year = names(output), type_b_values = output)
## Year type_b_values
## 1 1 11.0
## 2 2 12.5
這是使用plyr
的版本:
library(plyr)
ddply(dat, "Year", function(x) {
values.cols <- grep("value", names(x), value = TRUE)
a <- subset(x, type == "a", values.cols)
b <- subset(x, type == "b", values.cols)
c("type_b_values" = mean(b[a == 1]))
})
# Year type_b_values
# 1 1 11.0
# 2 2 12.5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.