How to mutate a new column by modifying another column?

不问归期 提交于 2020-03-23 12:00:15

问题


I have very simple question. I have a df like,

df1 <- data.frame(id=c(1,2,3))

I want to add a new column by adding 'z000' to existing id by using 'mutate' function of dplyr, My expectation is like,

df2 <- data.frame(id=c(1,2,3),new_id=c("z0001","z0002","z0003"))

Please let me know how to do.


回答1:


You just need paste0

df1 %>% 
mutate(new_id = paste0("z000", id))

  id new_id
1  1 z0001
2  2 z0002
3  3 z0003


来源:https://stackoverflow.com/questions/60638082/how-to-mutate-a-new-column-by-modifying-another-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!