简体   繁体   中英

Adding character to a column in data frame

Is there a way to add "." at the end of each cell of a column? something similar to: Add characters to a numeric column in dataframe but at the end of the value.

With paste , it is easier

df1$V1 <- paste0(df1$V1, ".")

Or using sprintf

df1$V1 <- sprintf("%s.", df1$V1)

You can use regex and sub and backreference:

a <- c("abc", "dfg", "hij", "xyz")
sub("^(.*)$", "\\1.", a)
[1] "abc." "dfg." "hij." "xyz."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM