简体   繁体   中英

Extracting information from a data frame in R

I have thought about a few approaches to this coding problem but none has yielded any results so far. I'm aware of the subset function but it seems inadequate to carry out the exact task below.

Given the data frame A below, I wish to obtain a second data frame with negative numbers closest to zero for each category changed to zero. For example for category A1, we have -4 changed to zero for category B10, -3 is closest to zero hence we change it to zero, etc.

For example:

x_1 <- c("A1", "A1", "B10", "B10", "B10", "C100", "C100", "C100")
y_1 <- c(1, -4, -10, -3, 5, 8, -9, -11)
A <- data.frame(x_1, y_1)

x_2 <-  c("A1", "A1", "B10", "B10", "B10", "C100", "C100", "C100")
y_2 <- c(1, 0, -10, 0, 5, 8, 0, -11)
B <- data.frame(x_2, y_2)

As you can see, I only want the negative numbers closest to zero in y_2 to be changed to zero whist other numbers can stay the same. It seems like a simple coding task and I'd appreciate any assistance with it.

Thanks

x_1 <- c("A1", "A1", "B10", "B10", "B10", "C100", "C100", "C100")
y_1 <- c(1, -4, -10, -3, 5, 8, -9, -11)
A <- data.frame(x_1, y_1)

x_2 <-  c("A1", "A1", "B10", "B10", "B10", "C100", "C100", "C100")
y_2 <- c(1, 0, -10, 0, 5, 8, 0, -11)
B <- data.frame(x_2, y_2)

Something like this?

library(plyr)
tmpfun <- function(d) {
    negy <- d[,2]<0    ## negative values
    w <- which.max(d[negy,2])  ## closest to zero
    d[negy,2][w] <- 0
    d
}
ddply(A,"x_1",tmpfun)

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