Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write csv or table variables to file

Tags:

r

Lets say I have a file like this:

a b c d
2 3 4 5
9 8 7 4
5 7 8 4

I would like to export column a and c but nothing else.

Can I do a version of write.csv or write.table

e.g. write.csv(myobject$a && myobject$b, file="outfile.csv")

like image 422
AWE Avatar asked Sep 06 '25 03:09

AWE


1 Answers

This should work

write.csv(myobject[,c("a","b")], file="outfile.csv",row.names=FALSE)

The brackets in myobject[rows,cols] select rows and columns of a data frame or matrix. If the rows argument is left empty, all rows are returned; and similarly for the "cols". A vector can be used to select multiple rows or columns. In this case, we're selecting all rows (because that part is blank) and columns "a" and "b".

The row.names=FALSE option prevents rownames from being printed. In some cases, you might want to keep them, of course.

like image 75
Frank Avatar answered Sep 08 '25 00:09

Frank