Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding single quotes to column in data frame

Tags:

r

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.

    Column x 111111 222222 333333 444444

What I wanted it to look like:

    Column x '111111', '222222', '333333', '444444',
like image 392
D.Kost Avatar asked Jul 16 '26 07:07

D.Kost


2 Answers

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])
like image 156
akrun Avatar answered Jul 18 '26 19:07

akrun


If your column is df$x you would do:

df$x <- paste0("'", df$x, "',")
like image 39
maccruiskeen Avatar answered Jul 18 '26 21:07

maccruiskeen