Questions tagged [write.table]

utils::write.table is an R function to write output to a file or a connection.

write.table prints its required argument x (after converting it to a data frame if it is not one nor a matrix) to a file or connection.

176 questions
102
votes
5 answers

write.table writes unwanted leading empty column to header when has rownames

check this example: > a = matrix(1:9, nrow = 3, ncol = 3, dimnames = list(LETTERS[1:3], LETTERS[1:3])) > a A B C A 1 4 7 B 2 5 8 C 3 6 9 the table displays correctly. There are two different ways of writing it to file... write.csv(a, 'a.csv')…
Yannick Wurm
  • 3,617
  • 6
  • 25
  • 28
61
votes
7 answers

Unimplemented type list when trying to write.table

I have the following data.table (data.frame) called output: > head(output) Id Title IsProhibited 1 10000074 Renault Logan, 2005 0 2 10000124 …
user1477388
  • 20,790
  • 32
  • 144
  • 264
40
votes
2 answers

Write a dataframe to csv file with value of NA as blank

There is a dataframe named cnbd, for example: cnbd = data.frame(1,2,3,NA,NA,5) Thus the expression: dim(cnbd)[1] give 1. I want to write a dataframe like cnbd to a csv with: write(file = filename, cnbd, append = TRUE) The problem comes: The…
Lawes
  • 609
  • 1
  • 8
  • 14
18
votes
2 answers

Crashing R when calling `write.table` on particular data set

The following consistently crashes my R session. Tested on two machines, Ubuntu and Mac OS X with similar results on both. Brief Description: Calling write.table on a data.frame with factor column of all NA's. The original data set is rather…
Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
16
votes
1 answer

Avoid quotation marks in column and row names when using write.table

I have the following data in a file called "data.txt": pid 1 2 4 15 18 20 1_at 100 200 89 189 299 788 2_at 8 78 33 89 90 99 3_xt 300 45 53 234 89 34 4_dx …
Layla
  • 5,234
  • 15
  • 51
  • 66
9
votes
2 answers

Do you always use row.names=F in write.csv? Changing the default values inside R (base) functions

Couldn't see a solution online but I thought this might be quite common. with write.csv I basically always have the argument row.name set to F. Is it possible to run a line once and update the default value of the argument for the rest of the…
nzcoops
  • 9,132
  • 8
  • 41
  • 52
7
votes
3 answers

How to prevent write.csv from changing POSIXct, dates and times class back to character/factors?

I have a .csv file with one field each for datetime, date and time. Originally they are all character fields and I have converted them accordingly. At the end of my code, if I do: str(data) I will get datetime: POSIXct date: Date time: Class…
4
votes
2 answers

Does R have a way of preserving column format when exporting a data frame?

I often export data.frame's in R but run into the problem when I try to import them back in, lose all of the formatting into date/numeric/logical/factor values and get it all back as character variables instead. It gets kind of old to have to run a…
jmi
  • 43
  • 4
4
votes
2 answers

Saving a matrix to a .txt file with proper alignment

I am trying to save a matrix that I have in R as a text file that is tab-delimited with the row and column names included and properly aligned. I have tried this: write.table(data, "mytable.txt", sep="\t", col.names=TRUE) But when I open the file…
Wolfpack
  • 59
  • 2
  • 7
3
votes
1 answer

write.csv in R: Something between

I have a question concerning write.csv or write.table - both did not bring what I wanted but produce the same: I need to produce a .csv file in an externally given format. The requirements are: Fields are always in plain text. Only if the field…
Irgendniemand
  • 184
  • 1
  • 9
3
votes
2 answers

Why R uses forward slash (/) and not backslash (\) in file paths

I was teaching an online course and a student asked me why R only uses / and not \ in file paths when using read.csv and other related functions. I tried looking at the documentation but it didn’t really mention anything about it. Never really…
Lalochezia
  • 497
  • 4
  • 15
3
votes
2 answers

Randomly split a data table and make output files in R

I want to randomly split a data table into n number of outputs; then I want to write.table those outputs for each list. So, in test I want to write a file for each list within test. library(data.table) set.seed(100) dt <-…
Connor Murray
  • 313
  • 3
  • 12
3
votes
0 answers

Write Table in R with a Set number of Decimal Places without changing to character data type

I want to use write.table to produce an output with 18 decimal places, in numeric format (for those columns that are Numeric) containing no NA values... For Example, in the table below - The Value Column would be classed as Numeric data type and…
3
votes
0 answers

How can I create text files after applying a function with lapply for multiple files?

I am a new R user. I want to perform the same set of multiple tasks for many files and write the results into text files. The code looks like this: ps_files<-dir(pattern = ".ps") lapply(ps_files, function(x) { #read files …
lala
  • 35
  • 5
3
votes
1 answer

How to generate random numbers to each .txt in write.table

I have question about how to generate random numbers for each .txt file generated by write.table command. I want to write different rows of random numbers to each .txt file. ref_file <- data.frame(id=seq(1:10)) # This creates 10 output file with…
Alexander
  • 4,527
  • 5
  • 51
  • 98
1
2 3
11 12