简体   繁体   中英

aggregate a column in a data frame

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM