How to add single quotes to strings in an R data frame column?


To add single quotes to strings in an R data frame column, we can use paste0 function. This will cover the strings with single quotes from both the sides but we can add them at the initial or only at the last position.

To add them on both the sides, we can use the following syntax −

Data_frame$Column<-paste0("'", Data_frame$Column,"'")

Example 1

Following snippet creates a sample data frame −

x<-LETTERS[1:20]
df1<-data.frame(x)
df1

Output

The following dataframe is created −

   x
1  A
2  B
3  C
4  D
5  E
6  F
7  G
8  H
9  I
10 J
11 K
12 L
13 M
14 N
15 O
16 P
17 Q
18 R
19 S
20 T

To add single quotes on both sides of each value in x, add the following code to the above snippet −

x<-LETTERS[1:20]
df1<-data.frame(x)
df1$x<-paste0("'",df1$x,"'")
df1

Output

If you execute all the above given codes as a single program, it generates the following output −

    x
1  'A'
2  'B'
3  'C'
4  'D'
5  'E'
6  'F'
7  'G'
8  'H'
9  'I'
10 'J'
11 'K'
12 'L'
13 'M'
14 'N'
15 'O'
16 'P'
17 'Q'
18 'R'
19 'S'
20 'T'

Example 2

Following snippet creates a sample data frame −

States_20<-c("Alabama", "Alaska", "American Samoa", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Guam", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky")
df2<-data.frame(States_20)
df2

Output

The following dataframe is created −

   States_20
1  Alabama
2  Alaska
3  American Samoa
4  Arizona
5  Arkansas
6  California
7  Colorado
8  Connecticut
9  Delaware
10 District of Columbia
11 Florida
12 Georgia
13 Guam
14 Hawaii
15 Idaho
16 Illinois
17 Indiana
18 Iowa
19 Kansas
20 Kentucky

To add single quotes on both sides of each value in States_20, add the following code to the above snippet −

States_20<-c("Alabama", "Alaska", "American Samoa", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Guam", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky")
df2<-data.frame(States_20)
df2$States_20<-paste0("'",df2$States_20,"'")
df2

Output

If you execute all the above given codes as a single program, it generates the following output −

   States_20
1  'Alabama'
2  'Alaska'
3  'American Samoa'
4  'Arizona'
5  'Arkansas'
6  'California'
7  'Colorado'
8  'Connecticut'
9  'Delaware'
10 'District of Columbia'
11 'Florida'
12 'Georgia'
13 'Guam'
14 'Hawaii'
15 'Idaho'
16 'Illinois'
17 'Indiana'
18 'Iowa'
19 'Kansas'
20 'Kentucky'

Example 3

Following snippet creates a sample data frame −

States_Abb_20<-c("AK", "AL", "AR", "AS", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "GU", "HI", "IA", "ID", "IL", "IN", "KS", "KY")
df3<-data.frame(States_Abb_20)
df3

Output

The following dataframe is created −

  States_Abb_20
1  AK
2  AL
3  AR
4  AS
5  AZ
6  CA
7  CO
8  CT
9  DC
10 DE
11 FL
12 GA
13 GU
14 HI
15 IA
16 ID
17 IL
18 IN
19 KS
20 KY

To add single quotes on both sides of each value in States_Abb_20, add the following code to the above snippet −

States_Abb_20<-c("AK", "AL", "AR", "AS", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "GU", "HI", "IA", "ID", "IL", "IN", "KS", "KY")
df3<-data.frame(States_Abb_20)
df3$States_Abb_20<-paste0("'",df3$States_Abb_20,"'")
df3

Output

If you execute all the above given codes as a single program, it generates the following output −

  States_Abb_20
1  'AK'
2  'AL'
3  'AR'
4  'AS'
5  'AZ'
6  'CA'
7  'CO'
8  'CT'
9  'DC'
10 'DE'
11 'FL'
12 'GA'
13 'GU'
14 'HI'
15 'IA'
16 'ID'
17 'IL'
18 'IN'
19 'KS'
20 'KY'

Updated on: 06-Nov-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements