Reputation: 943
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
df2 <- "V1 V2
21140 -2
3878 2
20434 -1"
Upvotes: 0
Views: 39
Reputation: 215137
Simply use if/else
condition check to summarize the column:
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.
Upvotes: 2