我有一个大型数据集,其中有来自单个人的纵向读数。 我想将信息随时间总结为一个二进制变量。即如果下面输入表中的diff> 5对于任何值,我想将对A的观察值减少到一个新列,说TRUE。
#Input
individual val1 val2 diff
A 32 36 -4
A 36 28 8
A 28 26 2
A 26 26 0
B 65 64 1
B 58 59 -1
B 57 54 3
B 54 51 3
#Output
individual newval
A TRUE
B FALSE
答案 0 :(得分:2)
多种方法:
在基数R中,我们可以使用aggregate
aggregate(diff~individual, df,function(x) any(x>5))
# individual diff
#1 A TRUE
#2 B FALSE
或tapply
tapply(df$diff > 5, df$individual, any)
我们也可以使用data.table
library(data.table)
setDT(df)[ ,(newval = any(diff > 5)), by = individual]
答案 1 :(得分:2)
使用dplyr
,您可以:
library(dplyr)
df %>%
group_by(individual) %>% # first group data
summarize(newval = any(diff > 5)) # then evaluate test for each group
#> # A tibble: 2 x 2
#> individual newval
#> <fct> <lgl>
#> 1 A TRUE
#> 2 B FALSE
df <- read.table(text = "individual val1 val2 diff
A 32 36 -4
A 36 28 8
A 28 26 2
A 26 26 0
B 65 64 1
B 58 59 -1
B 57 54 3
B 54 51 3
", header = TRUE)
答案 2 :(得分:1)
->paginate(10);
中带有$applications = $openRegDepDataReport->getReport($advertisers, $category)->paginate(10);
的选项
base R
或带有rowsum
rowsum(+(df1$diff > 5), df1$individual) != 0
by