Reputation: 55
I have a df with multiple columns that have many rows. I want to take one column and add single quotes around the values and a comma afterwards.
What I wanted it to look like:
Upvotes: 3
Views: 6714
Reputation: 2808
If your column is df$x
you would do:
df$x <- paste0("'", df$x, "',")
Upvotes: 3
Reputation: 4568
# data to build the df
Column x
a <- "111111"
b <- "222222"
c <- "333333"
# assemble the df
df <- data.frame(rbind(a,b,c))
names(df) <- "Column.X"
df$Column.X <- paste("'",df$Column.X,"',")
Upvotes: 0
Reputation: 886938
For a single quote, sQuote
can be used.
df1[,1] <- sQuote(df1[,1])
Or we can use sprintf
to include the '
and the ,
afterwards
df1[,1] <- sprintf("'%d',", df1[,1])
Upvotes: 5
Reputation: 844
You'll need to use
df$x <- paste0("'", df$x, "'", ",")
This should give you the desired result.
Upvotes: 3