0

我想向 a 添加一个新列,该列将第一列中的数值data.frame转换为后续匹配列的相应字符串(如果有),即列名与第一列中的该值部分匹配。

在此示例中,我希望为“Highest_Earner”添加一个值,该值取决于Earner_Number列中的值:

> df1 <- data.frame("Earner_Number" = c(1, 2, 1, 5),
                    "Earner5" = c("Max", "Alex", "Ben", "Mark"),
                    "Earner1" = c("John", "Dora", "Micelle", "Josh"))
> df1
  Earner_Number Earner5 Earner1
1             1     Max    John
2             2    Alex    Dora
3             1     Ben Micelle
4             5    Mark    Josh

结果应该是:

> df1
  Earner_Number Earner5 Earner1 Highest_Earner
1             1     Max    John           John
2             2    Alex    Dora        Neither
3             1     Ben Micelle       Michelle
4             5    Mark    Josh           Mark

我试过把它data.frame切成各种小块,但想知道是否有人有更清洁的方法?

4

2 回答 2

2
    #Have to convert them to character for nested if else to work.

    df$Earner5 <- as.character(df$Earner5)
    df$Earner1 <- as.character(df$Earner1)

    #Using nested if to get your column.
    df$Higher_Earner <-    ifelse(df$Earner_Number == 5, df$Earner5, 
                                      ifelse(df$Earner_Number==1df$Earner1,"Neither"))
于 2019-07-15T19:31:42.830 回答
0

dplyr 方法

library(tidyverse)

df <- tibble("Earner_Number" = c(1,2,1,5), "Earner5" = c('Max', 'Alex','Ben','Mark'), "Earner1" = c("John","Dora","Micelle",'Josh'))



df %>% 
  mutate(Highest_Earner = case_when(Earner_Number == 1 ~ Earner1,
                                    Earner_Number == 5 ~ Earner5,
                                    TRUE ~ 'Neither'))
于 2019-07-16T03:44:57.977 回答