How to combine files with paste and save to one of the files that is combined? - unix

Whenever I try to append to a file (with a table with the delimiter ";") with new data using the paste command, it never does what I intend to do.
I have tried the following;
paste -d";" Grades_HW1 Grades > Grades
but it overwrites the "Grades" file with Grades_HW1; when I try to append instead with ">>", it adds new lines to the bottom. How could I use paste to combine files and update it to the file?

Related

How to rename multiple files to the same file name and move to multiple folders using R?

I have around 500 files that all need to be named the same thing but need unique file path names.
So far I have been dropping each of the files manually into separate folders and renaming them, but this takes forever and I have to repeat the process each time I make a change to the original data.
To create the files I used the R code:
lapply(names(dfsplit), function(x){
write.csv(dfsplit[[x]], path = paste(x, ".csv", sep = ""))
})
Is there a way at this stage to create individual folders for each of the csv files and rename them e.g. 'name.csv' as they are created? Maybe using dir.create?
Let me know if this needs a clearer explanation.

Import data csv with particular quotes in R

I have a csv like this:
"Data,""Ultimo"",""Apertura"",""Massimo"",""Minimo"",""Var. %"""
"28.12.2018,""86,66"",""86,66"",""86,93"",""86,32"",""0,07%"""
What is the solution for importing correctly please?
I tried with read.csv("IT000509408=MI Panoramica.csv", header=T,sep=",", quote="\"") but it doesn't work.
Each row in your file is encoded as a single csv field.
So instead of:
123,"value"
you have:
"123,""value"""
To fix this you can read the file as csv (which will give you one field per row without the extra quotes), and then write the full value of that field to a new file as plain text (without using a csv writer).

csv not retaining format after splitting in R

I have a csv file say named abc.csv where name of a column is Component.Number and there are 20 different components I'm working with. The csv file contains 2000 entries which are sorted by component number. I'm using the following code to split the csv into 20 csv files where one file contains only data corresponding to a particular Component.Number.
abc = read.csv("abc.csv")
for (name in levels(abc$Component.Number)){
tmp=subset(abc,Component.Number==name)
#Create a new filename for each Component - the folder 'skews' should already exist in the same directory
fn=paste('skews/',gsub(' ','',name),sep='')
#Save the CSV file containing separate expenses data for each Component
write.table (tmp,fn,row.names=FALSE,sep = ",")
}
The code is working fine and I'm getting split files in the "skews" folder but the format of the files are not csv, in fact they don't have any file type. I have also tried write.csv instead of write.table in the last line, but no luck. So, how do I get the split files in .csv format and run the same R code on all of them using some kind of loop? The file names are those different component numbers. Thanks.

Text under generated table in .csv file in R

For weekly reports I generate, I create a .csv file for my outputs which has 9 columns and 7 rows.
I use this command to create my file:
write.csv(table, paste('home/weekly_',start.date,'.csv',sep=''), row.names=F)
Note: 'table' is a matrix (I believe that's the right R terminology)
I would like to add a footnote/note under the table in this file, would this be possible?
For example, if I were to create a text file instead of a .csv file, I would use the following commands:
cat("Number of participants not eligible:")
cat(length(which((tbl[,'Reg_age_dob']<=18) & as.Date(tbl[,'DateWithdrew'])>='2013-01-01'& as.Date(tbl[,'DateWithdrew'])<'2013-04-01' & as.Date(tbl[,'QuestionnaireEndDate'])<'2013-01-01' )))
cat("\n")
How would I do this to appear under the table in a .csv output file?
After writing the CSV part, just append the rest as new lines using
write("Footer",file="myfile",append=TRUE)
Solution from here: Add lines to a file
But be aware of the fact, that a CSV parser will be a upset, if you do not use comment tags correctly.
It might be better to use a second file for your purpose.

Appending to different columns of CSV in R

I have a CSV file and i m running two different shell scripts to write in to it. The header of the CSV looks like this.
date,C1,C2,C3
The first script writes the date in the date column. So it looks something like this
date,C1,C2,C3
2013-07-03,
Now i am stuck as to how to append to the C1,C2,C3 column using R, in the second shell script. I tried the following code
d<-data.frame(1,2,3)
write.table(d, file="my.csv", append=TRUE, col.names=FALSE, row.names=FALSE)
But its appending to the next line. I dont want to combine both the shell scripts. If there is a way to append to the respective columns through shell script also, that would be helpful also. Thank You in advance.
This looks over-complicated. Personally I avoid using append=TRUE and specially with different columns. One solution is to save the result of the first script, read it at the end of the second script and save once without appending.
Would something like this work for you?
cat(paste(d, collapse=","), file="my.csv", append=TRUE)

Resources