简体   繁体   中英

Export data frame as factors

New to R and first post here - this might be really obvious but I must be missing something.

Imported a csv with a binary dependent while

purchase=read.csv("../Desktop/purchase.csv", stringsASfactors=TRUE)

Now I wish to export the same data frame from R (in .csv), but instead keep the categorical variables (which were originally strings) as factors, effectively converting the data set.

I tried

write.csv(purchase,'../Desktop/purchaseconverted.csv', stringsASfactors=TRUE) 

but the write function doesn't support stringsASfactors.

I welcome help!

Using the factor() function, you can cast a vector as a categorical variable directly. Then write to csv.

purchase <- data.frame(a,b,c)
purchase['a'] <- factor(a)

write.csv(purchase,'../Desktop/purchaseconverted.csv')

Hope this helps.

The default behavior is going to treat factors as character strings when writing them to file. See this in the below example using the iris dataset, which contains a factor column Species :

write.csv(head(iris))
# "","Sepal.Length","Sepal.Width","Petal.Length","Petal.Width","Species"
# "1",5.1,3.5,1.4,0.2,"setosa"
# "2",4.9,3,1.4,0.2,"setosa"
# "3",4.7,3.2,1.3,0.2,"setosa"
# "4",4.6,3.1,1.5,0.2,"setosa"
# "5",5,3.6,1.4,0.2,"setosa"
# "6",5.4,3.9,1.7,0.4,"setosa"

You can change this by converting the factors to numeric, so the file contains the numeric values rather than the factor labels for the factor variable:

iris2 <- iris
iris2$Species <- as.numeric(iris2$Species)
> write.csv(head(iris2))
# "","Sepal.Length","Sepal.Width","Petal.Length","Petal.Width","Species"
# "1",5.1,3.5,1.4,0.2,1
# "2",4.9,3,1.4,0.2,1
# "3",4.7,3.2,1.3,0.2,1
# "4",4.6,3.1,1.5,0.2,1
# "5",5,3.6,1.4,0.2,1
# "6",5.4,3.9,1.7,0.4,1

This way you write the factor values, not the factor labels, to the CSV.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM