Row binding results in R while maintaining columns labels - r

I have a technical question in R:
how can I rowbind the following results (results1 and result2) into a data frame and keeping the columns labels for both:
result1:
meanAUC.SIM meanCmax.SIM meanTmax.SIM AUC.OBS Cmax.OBS Tmax.OBS PE.AUC PE.Cmax PE.Tmax
777.4444 74.64377 4.551254 820.7667 73.46508 3.089009 5.278274 1.604416 47.33703
result2:
medianAUC.SIM medianCmax.SIM medianTmax.SIM AUC.OBS Cmax.OBS Tmax.OBS PE.AUC PE.Cmax PE.Tmax
764.6611 72.4534 4.5 795.765 68.2 3 3.908683 6.236657 50
The reason behind this is that I want to write them in a *.csv file in an organized way with the correct labeling.

If the only reason you want to combine the data frames is to write them to a csv file, then you can instead just write each data frame separately to the same csv file. For example:
write.table(result1, "myfile.csv", row.names=FALSE, sep=",")
# If you want a blank row between them
cat("\n", file = "myfile.csv", append = TRUE)
write.table(result2, "myfile.csv", row.names=FALSE, sep=",", append=TRUE)
Here's what the file looks like:

Related

Add filename as column header when combining multiple files

I am combining 1 column of data from multiple/many text files into a single CSV file. This part is fine with the code I have. However, I would like to have after importing the filename (e.g., "roth_Aluminusa_E1.0.DPT") as the column header for the data column taken from that file. I know, similar questions have been asked but I can't work it out. Thanks for any help :-)
Code I am using which works for combining the files:
files3 <- list.files()
WAVELENGTH <- read.table(files3[1], header=FALSE, sep=",")[ ,1]
TEST9 <- do.call(cbind,lapply(files3,function(fn)read.table(fn, header=FALSE, sep = ",")[ , 2]))
TEST10 <- cbind(WAVELENGTH, TEST9)
You can do the following to add column names to TEST10. This assumes the column name you want for the first column is files3[1]
colnames(TEST10) <- c(files3[1], files3)
In case you want to keep the name of the first column as is, then we add the desired column names before binding WAVELENGTH with TEST9.
colnames(TEST9) <- files3
TEST10 <- cbind(WAVELENGTH, TEST9)
Then you can write to a csv as usual, keeping the column names as headers in the resulting file.
write.csv(TEST10, file = "TEST10.csv", row.names = FALSE)

How do I append data from a data frame in R to an Excel sheet that already exists

I have created dozens of data frames in R and would like to append them all to one sheet in an Excel file.
Here are two of the pages I have looked at in an attempt to find an answer (I don't have 10 reputations so I can't paste all four webpage urls I have visited):
Write data to Excel file using R package xlsx
The author says: "You can also add the dataframes to a particular starting place in the sheet using the startRow and startCol arguments to the addDataFrame function."
Here is the suggested code:
workbook.sheets workbook.test addDataFrame(x = sample.dataframe, sheet = workbook.test,
row.names = FALSE, startColumn = 4) # write data to sheet starting on line 1, column 4
saveWorkbook(workbook.sheets, "test.excelfile.xlsx") # and of course you need to save it.
Based on this suggestion, this was my attempt in RStudio:
addDataFrame(df_fl1, sheet = "AllData2.xlsx", startRow = 712)
This was R's output:
Error in sheet$getWorkbook : $ operator is invalid for atomic vectors
I've also tried this page:
Tutorial on Reading and Importing Excel Files into R
"If, however, you want to write the data frame to a file that already exists, you can execute the following command:"
write.xlsx(df,
"<name and extension of your existing file>",
sheetName="Data Frame"
append=TRUE)
write.xlsx(df_fl3, "AllData2.xlsx", sheetName="Salinity1", append=TRUE)
I tried this code and it overwrote the data that was already in the sheet. How can I append data from the data frames into an Excel sheet?
Appending to an existing Excel worksheet is a bit of a pain. Instead, read all of your Excel data files into R, combine them within R, and then write the single combined data frame to a new Excel file (or write to a csv file if you don't need the data to be in an Excel workbook). See code below for both the easy way and the hard way.
Easy Way: Do all the work in R and save a single combined data frame at the end
For example, if all of your Excel data files are in the current working directory and the first worksheet in each Excel file contains the data, you could do the following:
library(xlsx)
# Get file names
file.names = list.files(pattern="xlsx$")
# Read them into a list
df.list = lapply(file.names, read.xlsx, sheetIndex=1, header=TRUE)
Then combine them into a single data frame and write to disk:
df = do.call(rbind, df.list)
write.xlsx(df, "combinedData.xlsx", sheetName="data", row.names=FALSE)
Hard Way: Append successive data frames to a pre-existing Excel worksheet
Create a list of data frames that we want to write to Excel (as discussed above, in your actual use case, you'll read your data files into a list in R). We'll use the built-in iris data frame for illustration here:
df.list = split(iris, iris$Species)
To write each data frame to a single Excel worksheet, first, create an Excel workbook and the worksheet where we want to write the data:
wb = createWorkbook()
sheet = createSheet(wb, "data")
# Add the first data frame
addDataFrame(df.list[[1]], sheet=sheet, row.names=FALSE, startRow=1)
Now append all of the remaining data frames using a loop. Increment startRow each time so that the next data frame is written in the correct location.
startRow = nrow(df.list[[1]]) + 2
for (i in 2:length(df.list)) {
addDataFrame(df.list[[i]], sheet=sheet, row.names=FALSE, col.names=FALSE,
startRow=startRow)
startRow = startRow + nrow(df.list[[i]])
}
Save the workbook:
saveWorkbook(wb, "combinedData.xlsx")
addDataFrame is useful if you want to layout various summary tables in various parts of an Excel worksheet and make it all look nice for presentation. However, if you're just combining raw data into a single data file, I think it's a lot easier to do all the work in R and then just write the combined data frame to an Excel worksheet (or csv file) at the end.
To get around the original error that you mentioned:
Error in sheet$getWorkbook : $ operator is invalid for atomic vectors
You can try this:
wb <- loadWorkbook("<name and extension of your existing file>")
addDataFrame(df,getSheets(wb)$<sheetname>, startRow = 712)
saveWorkbook(wb, <filename>)

How to read every .csv file in R and export them into single large file

Hi so I have a data in the following format
101,20130826T155649
------------------------------------------------------------------------
3,1,round-0,10552,180,yellow
12002,1,round-1,19502,150,yellow
22452,1,round-2,28957,130,yellow,30457,160,brake,31457,170,red
38657,1,round-3,46662,160,yellow,47912,185,red
and I have been reading them and cleaning/formating them by this code
b <- read.table("sid-101-20130826T155649.csv", sep = ',', fill=TRUE, col.names=paste("V", 1:18,sep="") )
b$id<- b[1,1]
b<-b[-1,]
b<-b[-1,]
b$yellow<-B$V6
and so on
There are about 300 files like this, and ideally they will all compiled without the first two lines, since the first line is just id and I made a separate column to identity these data. Does anyone know how to read these table quickly and clean and format the way I want then compile them into a large file and export them?
You can use lapply to read all the files, clean and format them, and store the resulting data frames in a list. Then use do.call to combine all of the data frames into single large data frame.
# Get vector of files names to read
files.to.load = list.files(pattern="csv$")
# Read the files
df.list = lapply(files.to.load, function(file) {
df = read.table(file, sep = ',', fill=TRUE, col.names=paste("V", 1:18,sep=""))
... # Cleaning and formatting code goes here
df$file.name = file # In case you need to know which file each row came from
return(df)
})
# Combine into a single data frame
df.combined = do.call(rbind, df.list)

Generating csv format table in R

So I have a .csv file formatted as such:
Student,Grade
Steven,48
Tori,79
James,92
Elise,44
So I read it into R and manipulate the data a bit:
data = read.csv("/path/to/my.csv")
grades = data$Grade
grades = grades + 10
All I need to do now is write these new grades back to the grade column of my.csv, while preserving the formatting of the original csv (and the names). What is the most simple way to accomplish this?
Write the data back into the data.frame:
data$Grade <- grades ## you can actually skip the middle step here
Use write.table() with the correct settings:
write.table(data, file="/path/to/csv", sep=",", row.names=FALSE)
data$Grade <- data$Grade + 10
write.csv(data, file="/out/file/path.csv")

Write different data frame in one .csv file with R

I have 3 data frame, and I want them to be written in one single .csv file, one above the others, not in a same table. So, 3 different tables in one csv file. They all have same size.
The problem with write.csv: it does not contain "append" feature
The problem with write.table: csv files from write.table are not read prettily by Excel 2010 like those from write.csv
Post I already read and in which I could not find solution to my problem :
write.csv() a list of unequally sized data.frames
Creating a file with more than one data frame
Solution ?
write.csv just calls write.table under the hood, with appropriate arguments. So you can achieve what you want with 3 calls to write.table.
write.table(df1, "filename.csv", col.names=TRUE, sep=",")
write.table(df2, "filename.csv", col.names=FALSE, sep=",", append=TRUE)
write.table(df3, "filename.csv", col.names=FALSE, sep=",", append=TRUE)
Actually, you could avoid the whole issue by combining your data frames into a single df with rbind, then calling write.csv once.
write.csv(rbind(df1, d32, df3), "filename.csv")
We use sink files:
# Sample dataframes:
df1 = iris[1:5, ]
df2 = iris[20:30, ]
# Start a sink file with a CSV extension
sink('multiple_df_export.csv')
# Write the first dataframe, with a title and final line separator
cat('This is the first dataframe')
write.csv(df1)
cat('____________________________')
cat('\n')
cat('\n')
# Write the 2nd dataframe to the same sink
cat('This is the second dataframe')
write.csv(df2)
cat('____________________________')
# Close the sink
sink()

Resources