FKG FKG - 4 months ago 10
R Question

How to save data with selected variables in R?

Let's say I have data with 1000 variables. The dataset is a

.csv
and I read it using:
mydata<- read.csv2("dataset.csv", header=TRUE, sep=";")
.

How do I save
mydata
with selected variables? I usually use
write.csv2(mydata, "~/Desktop/newdata.csv")
-- is it possible to select certain variable within
write.csv2
? If not, what are the other options?

Answer

You can try this:

write.csv(dataframename[,c("a","b")], file="~/Desktop/newdata.csv",row.names=FALSE)

dataframename[rows,cols] select rows and columns of a data frame.