我有一个data.table,如下所示:
>DT
ID Year Value ABC_1 ABC_2 ABC_3
1: 3 2015 5 0 1 0
2: 4 2015 2 1 0 1
3: 5 2015 1 0 1 1
我想为每个ABC _...做的是:
> unique(DT[Year == 2015 & ABC_1 == 1, .(Year = Year, ABC = ABC_1, N = .N, MEAN = mean(Value))])
Year ABC N MEAN
1: 2015 1 1 2
> unique(DT[Year == 2015 & ABC_2 == 1, .(Year = Year, ABC = ABC_2, N = .N, MEAN = mean(Value))])
Year ABC N MEAN
1: 2015 1 2 3
> unique(DT[Year == 2015 & ABC_3 == 1, .(Year = Year, ABC = ABC_3, N = .N, MEAN = mean(Value))])
Year ABC N MEAN
1: 2015 1 2 1.5
我有超过20列的ABC _...我想把这个语句放在for循环中。我的问题是选择/规则需要列名。它无效:
> abc_name <- names(DT)[names(DT) %like% 'ABC']
> abc_name
[1] "ABC_1" "ABC_2" "ABC_3"
> abc_row<- data.table(Year=0, ABC=0, N=0, MEAN=0)
> for (i in 1: length(abc_name)){
+
+ temp_row <- unique(DT[Year == 2015 & abc_name[i] == 1, .(Year = Year, ABC = abc_name[i], N = .N, MEAN = mean(Value))])
+ abc_row <- rbind(abc_row, temp_row)
+ }
> abc_row
Year ABC N MEAN
1: 0 0 0 0
temp_row为空...
当我使用abc_name[I]
更改ABC_1
时,它可以正常运行:
> abc_name <- names(DT)[names(DT) %like% 'ABC']
> abc_name
[1] "ABC_1" "ABC_2" "ABC_3"
> abc_row<- data.table(Year=0, ABC=0, N=0, MEAN=0)
> for (i in 1: length(abc_name)){
+
+ temp_row <- unique(DT[Year == 2015 & ABC_1 == 1, .(Year = Year, ABC = ABC_1, N = .N, MEAN = mean(Value))])
+ abc_row <- rbind(abc_row, temp_row)
+ }
> abc_row
Year ABC N MEAN
1: 0 0 0 0
2: 2015 1 1 2
3: 2015 1 1 2
4: 2015 1 1 2
如何在我的脚本工作的for循环中使用abc_name
?
我希望你理解我的问题,有人可以帮助我。
答案 0 :(得分:0)
使用lapply
循环遍历名称向量(&#39; abc_name&#39;),在OP的帖子中应用逻辑,获取get
列的值和rbind
list
元素。{/ p>
lst <- lapply(abc_name, function(nm)
unique(DT[Year == 2015 & get(nm) == 1,
.(Year = Year, ABC = get(nm), N = .N, MEAN = mean(Value))]))
rbindlist(lst)
# Year ABC N MEAN
#1: 2015 1 1 2.0
#2: 2015 1 2 3.0
#3: 2015 1 2 1.5
或另一个选项是melt
来重塑广泛的&#39;长期&#39;格式,按&变量&#39;分组和&#39;年&#39;,并在&#39; i&#39;中指定逻辑索引。 (value==1
),总结数据集
melt(DT, measure = abc_name)[value==1, .(ABC=1, N= .N,
MEAN= mean(Value)), .(variable, Year)][, variable := NULL][]
# Year ABC N MEAN
#1: 2015 1 1 2.0
#2: 2015 1 2 3.0
#3: 2015 1 2 1.5