D.Kost
D.Kost

Reputation: 55

Adding single quotes to column in data frame

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',

Upvotes: 3

Views: 6714

Answers (4)

maccruiskeen
maccruiskeen

Reputation: 2808

If your column is df$x you would do:

df$x <- paste0("'", df$x, "',")

Upvotes: 3

Shawn Mehan
Shawn Mehan

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

akrun
akrun

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

Buzz Lightyear
Buzz Lightyear

Reputation: 844

You'll need to use

df$x <- paste0("'", df$x, "'", ",")

This should give you the desired result.

Upvotes: 3

Related Questions