用r中的多个条件替换行值

时间:2018-05-06 16:16:17

标签: r dataframe

目前我正在开展一个项目并陷入一个问题。我必须用不同列中的两个条件替换列的行值。假设:

x   y     m     n
1  200P  Jan  Perm
1  200T  Feb  Temp  
1  300P  Jan  Perm
2  200T  Feb  Temp      
2  300T  Feb  Temp
3  300P  Jan  Perm
3  400P  Jan  Perm

我想根据x和y更改列n的值。

for each x
check the value of y and n, if the first value of y with T is 
Perm/Temp. Replace the rest of the values of unique x rows to that 
value.

我试过但是当我执行代码时,它将所有Temp替换为Perm或Perm替换为Temp。但我希望它只更改该唯一x的行的值。有人可以帮我这个。我希望我的输出像:

x   y     m     n
1  200P  Jan  Temp
1  200T  Feb  Temp  
1  300P  Jan  Temp
2  200T  Feb  Temp      
2  300T  Feb  Temp
3  300P  Jan  Perm
3  400P  Jan  Perm

我正在尝试使用另一个具有不同条件的数据集。例如:

a   b    c       d
1   1   0.4    Minor
1   1   0.4    Minor
1   4   0.2    Minor
1   2   2.4    Major
2   4   0.2    Minor
3   1   0.4    Minor
3   4   0.2    Minor
3   4   4.2    Major

我试图在b列中将4替换为1,条件是如果c列为0.2。如果4和0.4在同一行,则将4替换为1.

3 个答案:

答案 0 :(得分:1)

我相信以下代码可以满足您的需求 它会创建一个新列n2,其值n对应于Ty的第一次出现。

fun <- function(DF){
    i <- grep("T", DF$y)[1]
    DF$n2 <- DF$n
    if(!is.na(i)) DF$n2[seq_len(nrow(DF))[-seq_len(i - 1)]] <- DF$n[i]
    DF$n2
}

res <- dat    # work with a copy
res$n2 <- unlist(lapply(split(dat[c(1:2, 4)], dat$x), FUN = fun))
res
#  x    y   m    n   n2
#1 1 200P Jan Perm Perm
#2 1 200T Feb Temp Temp
#3 1 300P Jan Perm Temp
#4 2 200T Feb Temp Temp
#5 2 300T Feb Temp Temp
#6 3 300P Jan Perm Perm
#7 3 400P Jan Perm Perm

如果您不想要新列,请执行

res$n <- res$n2
res <- res[-ncol(res)]

编辑。

显然我原来的代码是对的。以下是OP在上次评论中要求的内容。

fun2 <- function(DF){
    i <- grep("T", DF$y)[1]
    DF$n2 <- if(!is.na(i)) DF$n[i] else DF$n
    DF$n2
}

res2 <- dat    # work with a copy
res2$n2 <- unlist(lapply(split(dat[c(1:2, 4)], dat$x), FUN = fun))
res2
#  x    y   m    n   n2
#1 1 200P Jan Perm Temp
#2 1 200T Feb Temp Temp
#3 1 300P Jan Perm Temp
#4 2 200T Feb Temp Temp
#5 2 300T Feb Temp Temp
#6 3 300P Jan Perm Perm
#7 3 400P Jan Perm Perm

DATA。

dat <- read.table(text = "
x   y     m     n
1  200P  Jan  Perm
1  200T  Feb  Temp  
1  300P  Jan  Perm
2  200T  Feb  Temp      
2  300T  Feb  Temp
3  300P  Jan  Perm
3  400P  Jan  Perm
", header = TRUE)

编辑2。

根据问题编辑中的条件,使用逻辑索引要简单得多 请注意,在您的编辑中,您首先要说的是将列b的值从4更改为c0.2,但如果列c为{{},则表示要更改它1}}。以下代码使用0.4

0.2

数据2。

inx <- dat2$b == 4 & dat2$c == 0.2
dat2$b[inx] <- 1

答案 1 :(得分:1)

我们也可以尝试使用data.table

library(data.table)
i1 <- setDT(df1)[, {i1 <- grepl("T$", y)
            if(any(i1)) .I[which.max(i1):.N] } , x]$V1

或者

i1 <- setDT(df1)[, .I[cumsum(grepl("T$", y))!=0], x]$V1
df1[i1, n := first(n), x]
df1
#   x    y   m    n
#1: 1 200P Jan Perm
#2: 1 200T Feb Temp
#3: 1 300P Jan Temp
#4: 2 200T Feb Temp
#5: 2 300T Feb Temp
#6: 3 300P Jan Perm
#7: 3 400P Jan Perm

数据

df1 <- structure(list(x = c(1L, 1L, 1L, 2L, 2L, 3L, 3L), y = c("200P", 
"200T", "300P", "200T", "300T", "300P", "400P"), m = c("Jan", 
"Feb", "Jan", "Feb", "Feb", "Jan", "Jan"), n = c("Perm", "Temp", 
"Perm", "Temp", "Temp", "Perm", "Perm")), .Names = c("x", "y", 
"m", "n"), class = "data.frame", row.names = c(NA, -7L))

答案 2 :(得分:0)

您可以使用dplyr::first查找1st出现y的{​​{1}}值T,然后将n的所有值替换为找到的行中的值

library(dplyr)

df %>% group_by(x) %>%
  mutate(n = ifelse(!is.na(first(grep("T$",y))), 
                            n[first(grep("T$",y))], n )) %>%
  as.data.frame()

#   x    y   m    n
# 1 1 200P Jan Temp
# 2 1 200T Feb Temp
# 3 1 300P Jan Temp
# 4 2 200T Feb Temp
# 5 2 300T Feb Temp
# 6 3 300P Jan Perm
# 7 3 400P Jan Perm

数据:

df <- read.table(text = 
"x   y     m     n
1  200P  Jan  Perm
1  200T  Feb  Temp  
1  300P  Jan  Perm
2  200T  Feb  Temp      
2  300T  Feb  Temp
3  300P  Jan  Perm
3  400P  Jan  Perm",
header = TRUE, stringsAsFactors = FALSE)