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
- messed up headers
- empty cells are replaced with
NA
- when I open the file in a text editor all the values are shown as characters