changing first colname in a list of dataframes - r

I've got a list of dataframes and am trying to change the first colname using the lapply method
frames<-lapply(frames,function(x){ colnames(frames[[x]])[1]<-"date"})
is returning the error
Error in `*tmp*`[[x]] : invalid subscript type 'list'
I am not sure why it would produce this error as my understanding is that this should apply
colname[1]<-"date"
to every data frame in the list
If anyone can tell me the root of this error I would be very grateful!

You do not need to reference the frames list inside of lapply. Your function treats x as an element in the list, frames. Try this:
frames <- lapply(frames, function(x) { colnames(x)[1] <- "date"; return(x) })

Related

Combining For and If loop in R

I wish to merge tables in R only if that variable name exists. For the same, I have made a variable with the various table names that may or may not exist. And then added a "for" and "if" loop to combine the tables. All the tables if they exist, have a common "names" column. The code entered by me is as follows:
Designation.Attrition1<- data.frame(names)
x<- c("despivot2020new", "despivot2019new", "despivot2018new", "despivot2017new")
for( i in 1: length(x)){if (exists(x[i])){Designation.Attrition1<- merge(Designation.Attrition1, x[i] , by = "names")}}
However, I'm getting the error as "Error in fix.by(by.y, y) : 'by' must specify a uniquely valid column".
One of the reasons for the error, maybe that the merge function fails to consider the element of x as variable name.
x[i] is still a string and not a dataframe. Try to get the data first before merging.
for( i in seq_along(x)) {
if (exists(x[i])) {
Designation.Attrition1 <- merge(Designation.Attrition1,get(x[i]),by = 'names')
}
})

parLapply to apply user defined function to each list element in r

I am trying to parLapply() a function to each element of list "inputs". Each element is name of r workspace. Each workspace contains a dataframe with name same as workspace name. So,
1->loading workspace,
2-> removing extension using strsplit() to get data.frame name,
3->using as.name() to give it to a variable,
4->performing inner join using merge(),
5->rbind results of merge to a dataframe
6-> and removing unused variables from storage
The function I have written is below
library(parallel)
no_cores<-detectCores()
clust <- makeCluster(no_cores)
blockchain2009 <- data.frame()
load("blocktransact2009.RData")
inputs <- c("blockinputs1.RData","blockinputs2.RData")
parLapply(clust,inputs,process_inputs)
process_inputs <- function(i)
{
load(i)
i<-strsplit(i,"\\.")
blockinput<-as.name(i)
temp <- merge(x=blocktransact2009,y=blockinput,all.x=TRUE)
blockchain2009 <- rbind(blockchain2009,temp)
rm(temp,blockinput)
}
I get below, how to resolve. parLapply() should apply to each list element.
Error in checkForRemoteErrors(val) :
one node produced an error: 'list' object cannot be coerced to type 'symbol'

Error of unused argument when I am binding four dataframes using do.call and rbind

I am trying to merge four dataframes I had obtained earlier using the same rbind function:
EU_dist <- do.call("rbind",
EU_dist_objects_list)
WB_dist <- do.call("rbind",
WB_dist_objects_list)
WB_MDTF_dist <- do.call("rbind",
WB_MDTF_dist_objects_list)
NL_dist <- do.call("rbind",
NL_dist_objects_list)
So I want to combine the four dataframes using the line below:
ALL_dist <- do.call("rbind",WB_dist,NL_dist,WB_MDTF_dist,EU_dist)
Error in do.call("rbind", WB_dist, NL_dist, WB_MDTF_dist, EU_dist) :
unused argument (EU_dist)
Just to make sure EU_dist didn't have any issues, I have put at the end other WB, NL, or WB_MDTF _dist and I am always getting the error of unused argument (Last argument in the order of arguments in.
Any insight as to the cause of error and how I can change it will be highly appreciated

Naming elements of a list as a function of x in r

Here I am trying to name the individual elements of this list as a function of x so that I may index it later like one would with a dataframe or vector, yet I keep getting the error message
Error: unexpected '=' in "Indxlist <- sapply(1:1600, function(x) list( (x) ="
Here is the code that I am attempting to use...
Indxlist <- sapply(1:1600, function(x) list( (x) = dataframe1[,x]))
Thanks!
I think this cannot work. You cannot name a list with integers. Just do this after your command (which is not good practice anyways):
names(Indxlist) <- 1:1600

ldply with subset does not see local variable

i have a list of team names (teamNames) and a list of data frames (weekSummaries)
i want to get a list of team summaries by week:
teamSummaries <- llply(teamNames,getTeamSubset)
getTeamSubset = function(teamName){
temp=ldply(weekSummaries,subset,team_name==teamName)
}
however, when i run this i get an error
>Error in eval(expr, envir, enclos) : object 'teamName' not found
but when i run the command
>ldply(weekSummaries,subset,team_name=="Denver Broncos")
i get a data frame with the information i need for one team... can somebody point out what i'm doing wrong?
Looks like the answer is not to use the subset function, and instead to use a custom function, passing it the data frame, then subsetting using bracket notation. such as this:
teamSummaries <- llply(teamNames,getTeamSubset)
getTeamSubset = function(teamName){
temp=ldply(weekSummaries,function(week){
week[week$team_name==teamName,]
})
}

Resources