1

I am trying to create a series of data.frames and export them to a csv file to use in another program. The format of the file needs to be several columns of equal length, and then a final column that has no data in it. I'm thinking a list is the appropriate way to do this, but I cannot get the exported csv file to look like below.

alist <- list(c(100,1,2,3,4,5), list(c(200,1,2,3,4,5)), list(c(300)))

My final csv file should looks something like this:

100 200 300
1   1 
2   2 
3   3 
4   4 
5   5 

Any suggestions? Appreciate any help.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
Darkness
  • 11
  • 1
  • 3
  • 1
    Welcome to SO. Please share the code you've tried so far and the reasons why it is not meeting your expectations. Also, include a sample data set for us to play with. – Justin Nov 08 '13 at 15:53
  • I am familiar with using write.csv for dataframes, but I don't think it is possible to create a non-rectangular data frame without introducing NAs, which are not ok for my use. I thought that if I could create a list, and export each element as a column in a csv file, this would solve my problem. I have not been able to get anywhere close to being able to do this and have searched considerably. I have added an example list and what it should look like in the csv file, but beyond this I unfortunately do not have code that is worth posting. – Darkness Nov 08 '13 at 17:07
  • Any code is good. I'd use the solution from here: http://stackoverflow.com/questions/7196450/create-a-data-frame-of-unequal-lengths to create a data frame, then alist[is.na(alist)] <- "" – colcarroll Nov 08 '13 at 17:25
  • I have tried that, I don't believe you can have a data frame with nothing in it. Can't replace NAs with "". @JLLagrange – Darkness Nov 08 '13 at 17:36

1 Answers1

1

Hopefully I understand this question correctly (if Darkness returns...).

NA values in R should not pose any problem when writing to a CSV file since there is an argument in write.table, "na", described as the string to use for missing values in the data. Thus, your NA values can be replaced with nothing when writing your data.frame to a file.

Here's a small example:

Create an unbalanced data.frame

Notice the empty "300" column and the incomplete "400" column. In R, these are populated with NA values.

df <- data.frame(`100` = c(1,2,3,4,5), `200` = c(1,2,3,4,5), 
                 `300` = NA, `400` = c(letters[1:3], NA, NA), 
                 check.names=FALSE)
df
#   100 200 300  400
# 1   1   1  NA    a
# 2   2   2  NA    b
# 3   3   3  NA    c
# 4   4   4  NA <NA>
# 5   5   5  NA <NA>

Write the data to a CSV file

Here, I'm writing to a tempfile in the R workspace and viewing it with cat(readLines(...)) for convenience and demonstration. In actuality, you would probably be writing this to a CSV in your working directory and opening it with another program. Note the correct formation of the CSV file according to the input, but with nothing where an NA was in the original data.

x <- tempfile()
write.csv(df, file = x, na = "", row.names = FALSE)
cat(readLines(x), sep="\n")
# "100","200","300","400"
# 1,1,,"a"
# 2,2,,"b"
# 3,3,,"c"
# 4,4,,
# 5,5,,
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485