使用if / else如果需要在R中填充昵称

时间:2019-05-26 08:19:45

标签: r

我想在名称栏中查看昵称

我创建了向量(char),后者转换为data.frames。现在我想针对每个名称查看昵称

 y = c("Varun","Rivan","Jyothi","others")
  y = data.frame(y)
  y$nicknames = ""
  View(y)


   y$nicknames = ifelse(y$y =="Varun","Vast",(ifelse(y$y =="Jyothi","jo", 
 (ifelse(y$y =="Rivan","Kidoo","others")))))

它为我工作,但是需要查看简单易行的执行方式

需要在其他列中查看昵称

enter image description here

1 个答案:

答案 0 :(得分:0)

使用dplyr库中的case_when

y$nicknames <- case_when(
    y$y == "Varun" ~ "Vast",
    y$y == "Jyothi" ~ "jo",
    y$y == "Rivan" ~ "Kidoo",
    TRUE ~ "others"
)