1

I have a horizontally arranged CSV file (samplefile.csv) with headers are in the first column. Also, each row has a different number of columns. I want to read this CSV file, replace one of the cell value and save again as a CSV file with the same format as the original file with exactly same number of columns and rows. It sounds like a simple task, but I am struggling to find a way. I tried to do this with the help of this and this posts, but still, I can't get the output the way I want. Can somebody help me here?

Edit

my attempt using the answer in this post to read the CSV file (samplefile.csv) gives me the following output where headers are kinda messed up and empty cells are replaced with NA values which is not what I want

aaa <- read.tcsv("samplefile.csv")
aaa

     Header.1 Header.2..units. Header.3..units. Header.3..units..1
1   Some text           0.0625                0             264896
2        <NA>           0.0625             1200             664747
3        <NA>           0.0625             1380                  1
4        <NA>           0.2500             1500                  1
5        <NA>           0.6250             1620                 NA
6        <NA>           1.3125             1740                 NA
7        <NA>           2.4375             1860                 NA
8        <NA>           3.5625             1980                 NA
9        <NA>           4.6250             2100                 NA
10       <NA>           5.0000             2220                 NA
11       <NA>           5.0000             2340                 NA
12       <NA>           4.6250             2460                 NA
13       <NA>           3.5625             2580                 NA
14       <NA>           2.4375             2700                 NA
15       <NA>           1.3125             2820                 NA
16       <NA>           0.6250             2940                 NA
17       <NA>           0.2500             3060                 NA
18       <NA>           0.0625             3180                 NA
19       <NA>           0.0000             3300                 NA
20       <NA>           0.0000            18000                 NA

Also, I am not sure how to go back to original format when I save the file again after a modification (for example after replacing a cell value)

I tried saving the file again by using t (transpose) as given below

write.csv(t(aaa), file ="samplefile_e.csv", row.names=T)

but still, there are following issues in the saved file

  1. messed up headers
  2. empty cells are replaced with NA
  3. when I open the file in a text editor all the values are shown as characters
rm167
  • 1,185
  • 2
  • 10
  • 26
  • 1
    What code did you try exactly? How did your attempts fail? You should include a minimal [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) in the question itself (don't link to an external dropbox file). Show exactly how you are trying to make a change to the data. – MrFlick Sep 26 '18 at 14:52
  • I added some more details on my attempt. Thanks – rm167 Sep 26 '18 at 15:12
  • Where does the `read.tcsv` function come from? That's not a base R function. How exactly are you saving the file? – MrFlick Sep 26 '18 at 15:13
  • More details added. The `read.tcsv` function is from the only answer to the post for which I provided the link. Thanks. – rm167 Sep 26 '18 at 15:32

1 Answers1

0

I finally got a solution from Bill Dunlap in R- help mail list.

In his words

Using read.table to read data with a variable number of entries per row is probably a mistake - data.frames (which read.table returns) are not meant for this sort of data.You want to store the data in R as a list with names.

Complete answer can be found here

rm167
  • 1,185
  • 2
  • 10
  • 26