How to create a character vector from data frame values in R?

R ProgrammingServer Side ProgrammingProgramming

To create a character vector in R we can enclose the vector values in double quotation marks but if we want to use a data frame values to create a character vector then as.character function can be used. For example, if we have a data frame df then all the values in the df can form a character vector using as.character(df[]).

Example1

 Live Demo

x1<−letters[1:10]
x2<−letters[11:20]
df1<−data.frame(x1,x2)
df1

Output

x1 x2
1 a k
2 b l
3 c m
4 d n
5 e o
6 f p
7 g q
8 h r
9 i s
10 j t
as.character(df1[])
[1] "c(\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\")"
[2] "c(\"k\", \"l\", \"m\", \"n\", \"o\", \"p\", \"q\", \"r\", \"s\", \"t\")"
is.vector(as.character(df1[]))
[1] TRUE

Example2

 Live Demo

set.seed(3232)
y1<−sample(LETTERS[1:5],20,replace=TRUE)
y2<−sample(LETTERS[6:15],20,replace=TRUE)
y3<−sample(LETTERS[16:26],20,replace=TRUE)
df2<−data.frame(y1,y2,y3)
df2

Output

y1 y2 y3
1 E O U
2 B N U
3 C L P
4 A N Q
5 A I W
6 E M Y
7 E N P
8 B I Z
9 A G Z
10 B J W
11 D L R
12 D G R
13 B M U
14 D K W
15 B F S
16 A O Y
17 D K Z
18 A N Y
19 A O U
20 D K W
as.character(df2[])
[1] "c(\"E\", \"B\", \"C\", \"A\", \"A\", \"E\", \"E\", \"B\", \"A\", \"B\", \"D\", \"D\", \"B\", \"D\", \"B\", \"A\", \"D\", \"A\", \"A\", \"D\")"
[2] "c(\"O\", \"N\", \"L\", \"N\", \"I\", \"M\", \"N\", \"I\", \"G\", \"J\", \"L\", \"G\", \"M\", \"K\", \"F\", \"O\", \"K\", \"N\", \"O\", \"K\")"
[3] "c(\"U\", \"U\", \"P\", \"Q\", \"W\", \"Y\", \"P\", \"Z\", \"Z\", \"W\", \"R\", \"R\", \"U\", \"W\", \"S\", \"Y\", \"Z\", \"Y\", \"U\", \"W\")"
is.vector(as.character(df2[]))
[1] TRUE

Example3

 Live Demo

z1<−sample(c("Purity","Impurity","Crystal"),20,replace=TRUE)
z2<−sample(c("Chain Reaction","Odorless","Reactive"),20,replace=TRUE)
df3<−data.frame(z1,z2)
df3

Output

z1 z2
1 Impurity Reactive
2 Crystal Reactive
3 Impurity Chain Reaction
4 Purity Chain Reaction
5 Impurity Chain Reaction
6 Crystal Reactive
7 Crystal Chain Reaction
8 Impurity Reactive
9 Purity Odorless
10 Impurity Chain Reaction
11 Purity Odorless
12 Purity Chain Reaction
13 Impurity Odorless
14 Impurity Chain Reaction
15 Impurity Odorless
16 Purity Odorless
17 Impurity Chain Reaction
18 Crystal Reactive
19 Impurity Chain Reaction
20 Crystal Reactive
as.character(df3[])
[1] "c(\"Impurity\", \"Crystal\", \"Impurity\", \"Purity\", \"Impurity\", \"Crystal\", \"Crystal\", \"Impurity\", \"Purity\", \"Impurity\", \"Purity\", \"Purity\", \"Impurity\", \"Impurity\", \"Impurity\", \"Purity\", \"Impurity\", \"Crystal\", \"Impurity\", \"Crystal\")"
[2] "c(\"Reactive\", \"Reactive\", \"Chain Reaction\", \"Chain Reaction\", \"Chain Reaction\", \"Reactive\", \"Chain Reaction\", \"Reactive\", \"Odorless\", \"Chain Reaction\", \"Odorless\", \"Chain Reaction\", \"Odorless\", \"Chain Reaction\", \"Odorless\", \"Odorless\", \"Chain Reaction\", \"Reactive\", \"Chain Reaction\", \"Reactive\")"
is.vector(as.character(df3[]))
[1] TRUE
raja
Published on 07-Nov-2020 11:44:12
Advertisements