简体   繁体   中英

adding space in a column of a dataframe in R

I want to replace "Qu.:" with "Qu. :" for the entire column. But below code doesnt run :(

emp = c('Min.   :2014-07-17 00:00:00','1st Qu.:2014-07-17 00:00:00')
df <- data.frame(emp)

for (i in (1 : nrow(df))) { gsub("Qu.:", "Qu. :", df[i,1] ) }

sub and gsub are vectorized, so no need for a loop. Give this a try:

df$emp <- sub("Qu.:", "Qu. :", df$emp, fixed = TRUE)

sub matches and replaces only the first occurrence, which is what it seems like what you're doing on that column. It can be more efficient than gsub and is more appropriate if you're only matching the first occurrence.

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