df1 <- read.table(text = "V1 V2
21140 -2
21140 0
21140 2
21140 -1
3878 0
3878 1
3878 2
20434 -1
20434 2
20434 1", header = TRUE)
for getting the mean i used the following command
为了获得平均值我使用了以下命令
sample.test.final <- df1[,list(V2 = mean(V2)), by = c("V1")]
Now i want the following results
现在我想要以下结果
condition : if the value of V2 for corresponding V1 is negative select that value or else use the max positive value
条件:如果相应V1的V2值为负,则选择该值或使用最大正值
df2 <- "V1 V2
21140 -2
3878 2
20434 -1"
1 个解决方案
#1
2
Simply use if/else
condition check to summarize the column:
只需使用if / else条件检查来汇总列:
df1[, .(V2 = if(all(V2 >= 0)) max(V2) else V2[V2 < 0]), V1]
# V1 V2
#1: 21140 -2
#2: 21140 -1
#3: 3878 2
#4: 20434 -1
Note this keeps all negative values in V2
if there's any.
注意,如果有的话,这会将所有负值保存在V2中。
#1
2
Simply use if/else
condition check to summarize the column:
只需使用if / else条件检查来汇总列:
df1[, .(V2 = if(all(V2 >= 0)) max(V2) else V2[V2 < 0]), V1]
# V1 V2
#1: 21140 -2
#2: 21140 -1
#3: 3878 2
#4: 20434 -1
Note this keeps all negative values in V2
if there's any.
注意,如果有的话,这会将所有负值保存在V2中。