问题
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