根据上一列中的值创建新列

时间:2019-07-15 19:04:04

标签: r dataframe derived partial-matches

我想在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切成小块,但想知道是否有人使用一种更清洁的方法?

2 个答案:

答案 0 :(得分: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"))

答案 1 :(得分: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'))