I have a very big data frame and multiple copies of it that I programmed R to read automatically. I am now trying figure a way to read the data points automatically as well.
I previously had the code written as the following when it was reading a single csv file that I named "mydata":
subject_variable = mydata$X0[1]
size_variable = mydata$X29[1]
vertical_movement =
if (mydata$X37[1] == 0)
{
"None"
}else{
"Moving"
}
horizontal_start= mydata$X36[1]
Instead of a single data file, I currently have a list of data files. How would the code I wrote above would change so it would read the list of data files that I have?
Would I have to use a for loop for each variable? or would it work with just one for loop?
Thanks!
From your code, it seems like mydata is a dataframe, so as long as you have a list of dataframes, the following approach will work well:
#create a function with the process you've already done for one of the dataframes
fn <- function(mydata){
subject_variable = mydata$X0[1]
size_variable = mydata$X29[1]
vertical_movement =
if (mydata$X37[1] == 0)
{
"None"
}else{
"Moving"
}
horizontal_start= mydata$X36[1]
}
#do that for all the dataframes
lapply(fn, list_of_dataframes)
If you have a list of file locations, you can also use lapply along with read_csv (or similar) to create a list of dataframes and use the above approach.
Related
I want to manipulate different .csv files through a loop and a list. Works fine, but I for the output, I have to create many .xlsx files and the files have to be named according to the value of a certain variable.
I've already tried piping the write_xlsx function with ifelse condition like:
for (i in 1:length(files)) {
files[[i]] %>%
write_xlsx(files[[i]], paste(ifelse(x="test1", "/Reportings/test1.xlsx",
ifelse(x="test2", "/Reportings/test2.xlsx", "test3")
}
I expect that multiple .xlsx files will be created in the folder Reportings.
Not easy to answer precisely with the information you gave, but here is a minimal example that seems to do what you want :
According that your list is composed of matrix, that x is a variable and that it always has the same value.
df=data.frame(x=rep("test1",3),y=rep("test1",3))
df2=data.frame(x=rep("test2",3),y=rep("test2",3))
files=list(df,df2)
files[[1]]$x[1]
for(i in 1:length(files)){
write.xlsx(files[[i]],paste0("Reportings/",files[[i]]$x[1],".xlsx"))
}
I am trying to loop through a list of data frames to quickly check a bunch of data all at once.
source_files <- list.files(pattern = "\\.csv$")
got a list of csv files in my working directory
for (i in source_files) {
assign(substr(i,1,nchar(i)-4), read.csv(i))
}
use a for loop to read all the csv files
for (i in source_files) {
n <- substr(i,1,nchar(i)-4)
glimpse(n)
}
however this doesn't work. I tried it with a few functions like glimpse, colnames...etc and none of them seem to work. glimpse returns chr "2021xxxdata"
i want to check them before i combine them with bind_rows. any better way to do this? or a way to make this work?
I am trying to write multiple dataframes to multiple .csv files dynmanically. I have found online how to do the latter part, but not the former (dynamically define the dataframe).
# create separate dataframes from each 12 month interval of closed age
for (i in 1:max_age) {assign(paste("closed",i*12,sep=""),
mc_masterc[mc_masterc[,7]==i*12,])
write.csv(paste("closed",i*12,sep=""),paste("closed",i*12,".csv",sep=""),
row.names=FALSE)
}
In the code above, the problem is with the first part of the write.csv statement. It will create the .csv file dynamically, but not with the actual content from the table I am trying to specify. What should the first argument of the write.csv statement be? Thank you.
The first argument of write.csv needs to be an R object, not a string. If you don't need the objects in memory you can do it like so:
for (i in 1:max_age) {
df <- mc_masterc[mc_masterc[,7]==i*12,])
write.csv(df,paste("closed",i*12,".csv",sep=""),
row.names=FALSE)
}
and if you need them in memory, you can either do that separately, or use get to return an object based on a string. Seperate:
for (i in 1:max_age) {
df <- mc_masterc[mc_masterc[,7]==i*12,])
assign(paste("closed",i*12,sep=""),df)
write.csv(df,paste("closed",i*12,".csv",sep=""),
row.names=FALSE)
}
With get:
for (i in 1:max_age) {
assign(paste("closed",i*12,sep=""), mc_masterc[mc_masterc[,7]==i*12,])
write.csv(get(paste("closed",i*12,sep="")),paste("closed",i*12,".csv",sep=""),
row.names=FALSE)
}
I'm having some trouble writing a function to read in multiple .xlsx files as separate data frames in R with a for loop. When I run the function nothing happens. No Errors, but no data frames are loaded into R either. When I take the assign piece out of the function and manually change the input from the for loop to an example the assign function works. Here is an example of the code:
library(readxl)
Load<-function(File_Path,Samp){
setwd(File_Path)
for (i in 1:length(Samp)){
assign(paste("Sample_",Samp[i],sep = ""),read_excel(paste("Sample_",Samp[i],".xlsx",sep = "")))
}
}
Load(File_Path = "~/Desktop/Test",Samp = "A") # Doesn't Work
#When this piece is taken out of the loop and the Sample ("A") replaced it works.
assign(paste("Sample_","A",sep = ""),read_excel(paste("Sample_","A",".xlsx",sep = ""))) # Does Work
In reality there is a long list of samples to load and want to do so by assigning a list to "Samp" such as c("A","C","D"). Thanks up front for any assistance.
You can fix your problem by adding inherits=TRUE to assign
I have a folder with multiple files to load:
Every file is a list. And I want to combine all the lists loaded in a single list. I am using the following code (the variable loaded every time from a file is called TotalData) :
Filenames <- paste0('DATA3_',as.character(1:18))
Data <- list()
for (ii in Filenames){
load(ii)
Data <- append(Data,TotalData)
}
Is there a more elegant way to write it? For example using apply functions?
You can use lapply. I assume that your files have been stored using save, because you use load to get them. I create two files to use in my example as follows:
TotalData<-list(1:10)
save(TotalData,file="DATA3_1")
TotalData<-list(11:20)
save(TotalData,file="DATA3_2")
And then I read them in by
Filenames <- paste0('DATA3_',as.character(1:2))
Data <- lapply(Filenames,function(fn) {
load(fn)
return (TotalData)
})
After this, Data will be a list that contains the lists from the files as its elements. Since you are using append in your example, I assume this is not what you want. I remove one level of nesting with
Data <- unlist(Data,recursive=FALSE)
For my two example files, this gave the same result as your code. Whether it is more elegant can be debated, but I would claim that it is more R-ish than the for-loop.