0

I have a data frame df with about 50 columns and 20.000 rows. It looks like the data frame below:

Date                  P1               P2            P3            P4
1/1/2000               0               0.4           0             0
2/1/2000               0               0.1           0             0.1
3/1/2000               0.5             0             0             1
4/2000                 0.8             1.5           1             1

How can I export every (numeric) column to a text file?

(except for the Date column, which I could remove/subset/delete from df)

I would like the text file to have the same name as the column header.

P1.txt:

0             
0              
0.5            
0.8            

P2.txt:

0.4             
0.1              
0            
1.5            

etc.

This what I have so far, for 50 columns:

     df$Date<-NULL
     for(i in c(1:32)){
     write.table(df[,i],file=paste0(names(df)[i],row.names = FALSE, col.names = FALSE, ".txt")) 

}

however the generated output is: P1.txt:

"P1"
"1"  0             
"2"  0              
"3"  0.5            
"4"  0.8  

is it possible to get rid of this first column"1","2","3","4" and header "P1"?

T. BruceLee
  • 501
  • 4
  • 16
  • please create a [reproducible example](http://stackoverflow.com/q/5963269/3250126). `dput()` should help with that – loki Nov 06 '16 at 22:15
  • 1
    You just need to find the numeric columns, then loop `write.table` over them. I would use `Map()` to make it easier to match the names to the data. If you wish to post your attempt, we can help you fix it. But not showing any effort is a good way to get downvotes. – Rich Scriven Nov 06 '16 at 22:20
  • Isn't this a basic problem of subsetting and exporting data? I think both are already answered on SO. – loki Nov 06 '16 at 22:25
  • Possible duplicate of [split dataframe into multiple output files in r](http://stackoverflow.com/questions/10002021/split-dataframe-into-multiple-output-files-in-r) – PereG Nov 06 '16 at 22:36
  • @PereG it is not a duplicate of your link. – zx8754 Nov 06 '16 at 22:42
  • @zx8754 The answer offered, you just have to preselect numeric columns and column name to save. – PereG Nov 06 '16 at 22:48
  • @PereG you link is splitting data by one column values `k, l, c`, in this case, every column must be output in a separate file, `P1, P2, ..etc`. – zx8754 Nov 06 '16 at 22:53
  • T. BruceLee, all columns are numeric? @zx8754 I see. – PereG Nov 06 '16 at 23:00

2 Answers2

3
# dummy data
mydf <- as.data.frame(matrix(c(rnorm(20), rnorm(20), rnorm(20), rnorm(20), rnorm(20)), nrow=20))
        V1          V2          V3          V4          V5

1  -0.62829066  1.15406529 -0.18567863  1.59192254  0.95744852
2   0.47275575  1.57982778 -0.78190000  0.56729278 -0.99929298
...
20  0.30557789  0.55654245  0.43422811  0.22404488 -0.39800789

write.csv(mydf[, 1], file = paste0(names(mydf)[1], ".csv")) 

This creates V1.csv in R's working directory.

(l)applying this creates V1.csv, V2.csv, ..., V5.csv:

lapply(1:5, function(x) write.csv(mydf[, x], file = paste0(names(mydf)[x], ".csv", row.names = FALSE, col.names = FALSE, quote = FALSE)))

Now, V1.csv, V2.csv, ..., V5.csv are created in R's working directory that you can find via getwd().

The above one-liner prints [[1]] NULL [[2]] NULL ...[[5]] NULL to the console along with creation of V1.csv, V2.csv, ..., V5.csv in R's working directory.

Erdogan CEVHER
  • 1,788
  • 1
  • 21
  • 40
  • Thanks for the help. But this option generates the columnname in my ouput file and it also generates an additional first column with id numbers. Is it possible in R to generate an output.txt file that contains only one column with the data, as given in the example. so I would like the.. lets call it " column zero" left out and no column header in my output.txt file – T. BruceLee Nov 06 '16 at 22:50
  • 2
    @T.BruceLee yes, read the manual for `?write.csv`, you can set `row.names = FALSE, col.names = FALSE, quote = FALSE`. – zx8754 Nov 06 '16 at 22:54
  • @zx8754 I tried adding the *row.names = FALSE* and *col.names = FALSE* statements. All it seems to do is add FALSE behind the name of the text file for every time I use it. So in stead of the name being *P1* it is *P1FALSEFALSE*, it didn't change the content of the output.txt. But I am probably not using it right? sorry I am new to R, I get that its basic stuff probably. – T. BruceLee Nov 06 '16 at 23:28
  • 1
    `row.names` and `col.names` are arguments of `write.table`, not a `file` argument. – PereG Nov 06 '16 at 23:38
  • @T.BruceLee Try `lapply(which(sapply(df, is.numeric)), function(x) write.table(df[x], file = paste0(names(df[x]), ".txt"), row.names = F, col.names=F) )` And that works only for numeric columns – PereG Nov 06 '16 at 23:39
  • @PereG, i solved it. It was basic, I had misplaced row.names. I had to place row.names after ***df[,i],***. I will edit my basic solution in my question. Thanks for the help! – T. BruceLee Nov 06 '16 at 23:39
  • @zx8754 I applied your addition. Thx a lot. – Erdogan CEVHER Nov 07 '16 at 04:16
1

This is what I used as solution:

### remove date column
df7$Date<-NULL

### loop writing text files, for 50 columns, without column and row names, and use the columnnames as text file names.  
for(i in c(1:50)){
write.table(df7[,i],row.names = FALSE, col.names = FALSE,file=paste0(names(df7)[i],".txt"))
}
T. BruceLee
  • 501
  • 4
  • 16