条件列格式

时间:2018-04-26 12:53:59

标签: r data-cleaning

我有一个如下所示的数据框:

  cat df1 df2 df3
1   1  NA   1  NA
2   1  NA   2  NA
3   1  NA   3  NA
4   2   1  NA  NA
5   2   2  NA  NA
6   2   3  NA  NA

我想填充df3,这样当cat = 1时,df3 = df2,当cat = 2时,df3 = df1。但是我收到了一些不同的错误消息。

我目前的代码如下:

df$df3[df$cat == 1] <- df$df2
df$df3[df$cat == 2] <- df$df1

2 个答案:

答案 0 :(得分:2)

试试这段代码:

df[df$cat==1,"df3"]<-df[df$cat==1,"df2"]
df[df$cat==2,"df3"]<-df[df$cat==1,"df1"]

输出:

df
      cat df1 df2 df3
    1   1   1   1   1
    2   2   1   2   1
    3   3   1   3  NA
    4   4   2  NA  NA
    5   5   2  NA  NA
    6   5   2  NA  NA

答案 1 :(得分:0)

你可以尝试

ifelse(df$cat == 1, df$df2, df$df1)
[1] 1 2 3 1 2 3

# saving 
df$df3 <- ifelse(df$cat == 1, df$df2, df$df1)

# if there are other values than 1 and 2 you can try a nested ifelse 
# that is setting other values to NA
ifelse(df$cat == 1, df$df2, ifelse(df$cat == 2, df$df1, NA))

# or you can try a tidyverse solution. 
library(tidyverse) 
df %>% 
  mutate(df3=case_when(cat == 1 ~ df2, 
                       cat == 2 ~ df1))
cat df1 df2 df3
1   1  NA   1   1
2   1  NA   2   2
3   1  NA   3   3
4   2   1  NA   1
5   2   2  NA   2
6   2   3  NA   3

# data
df <- structure(list(cat = c(1L, 1L, 1L, 2L, 2L, 2L), df1 = c(NA, NA, 
                           NA, 1L, 2L, 3L), df2 = c(1L, 2L, 3L, NA, NA, NA), df3 = c(NA, 
                           NA, NA, NA, NA, NA)), .Names = c("cat", "df1", "df2", "df3"), class = "data.frame", row.names = c("1", 
                           "2", "3", "4", "5", "6"))