- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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'
- Related Articles
- How to rename a single column in an R data frame?
- How to extract a single column of an R data frame as a data frame?
- How to remove single quote from string column in an R data frame?
- How to convert multiple columns into single column in an R data frame?
- How to add a column in an R data frame with consecutive numbers?
- How to separate two values in single column in R data frame?
- How to add rows in an R data frame?
- How to update single value in an R data frame?
- How to add a column between columns or after last column in an R data frame?
- How to add one column in an R data frame to rest of the next columns?
- How to add a new column in an R data frame with count based on factor column?
- How to filter column values for some strings from an R data frame using dplyr?
- How to add a string before each numeric value in an R data frame column?
- How to create a boxplot of single column in R data frame with column name?
- How to standardized a column in an R data frame?
