Fabio
Fabio

Reputation: 274

Keep quotes when creating data frame in R

I have a data frame df containing many columns. From these, I extract two (col1 and col2) and use df2 = data.frame(df$col1, df$col2) for this.

It works: a new dataframe made of those two columns is created. But df$col1 was made of strings as:

"test1"
"test2"

df2$col1 is made instead of values (not sure how to call them) as:

test1
test2

Intersection between these df$col1 and df2$col1 yields zero. How do I keep the column as a string in the new data frame?

I tried adding stringsAsFactors = FALSE but nothing changed.

Upvotes: 0

Views: 258

Answers (2)

Murugesan
Murugesan

Reputation: 131

'df' is your data frame and you do not want to change the original data type. i.e., you should retain your string data type.

So basically you should subset those columns from the original data frame instead of creating a new data frame using 'data.frame'.

> df2<-df[,c("col1","col2")]

You can check the data type of each column in data frame by

> str(df2)

Upvotes: 1

Roman Luštrik
Roman Luštrik

Reputation: 70623

Your first data.frame has col1 set as character. When you create a second data.frame, this character column is coerced to factor. Here's a possible short proof.

> df1 <- data.frame(col1 = c("a", "b", "c"), col2 = 1:3)
> df1$col1
[1] a b c
Levels: a b c
> df1$col1 <- as.character(df1$col1)
> df1$col1
[1] "a" "b" "c" # this is what you have
> 
> df2 <- data.frame(col1 = df1$col1)
> df2$col1
[1] a b c # coerced to factor
Levels: a b c

Upvotes: 1

Related Questions