number of rows each data frame in a list [duplicate] - r

This question already has answers here:
How to count rows?
(3 answers)
Closed 5 years ago.
I have a list of data frames in R. They are created using lapply and read.csv commands. I want to know the number of rows of each data frame in the list. The nrow command does not work for this list. I am not sure may because it is a list. Any idea about counting the number of rows of each data frame in a list?

Lists don't have rows. Use sapply to loop over the list:
sapply(data.frame.list, nrow)

or lapply- the difference is in the output
lapply(data.frame.list, function(x) nrow(x))

Related

How to loop through specific data frames based on a List and add them all in a rbind function using R? [duplicate]

This question already has answers here:
Combine several data frames in the global environment by row (rbind)
(2 answers)
How to rbind all the data.frames in your working environment?
(1 answer)
Closed 2 years ago.
I have 5 dataframes all with the same heading and columns
df1,df2,df3,df4,df5
I created a list based on the dataframe names
List<-c("df1","df2","df3","df4",d"f5")
I want to create an rbind looping the df's based off the list. i tried this
ALL_dfs<-lapply(List,rbind)
but this doesn't read it as a combined dataframe by rbind function.
I think the most straightforward option is this:
do.call(rbind, mget(List))
?mget
Search by name for an object (get) or zero or more objects (mget).
The other option, if possible, is to store your data.frames in a list when you create them. Not always an option, but may be possible with some planning
With map_df from purrr.
library(purrr)
all_dfs <- map_dfr(.x = List, .f = bindr)

sorting rows of data frames which are in a list in R [duplicate]

This question already has answers here:
How to sort all dataframes in a list of dataframes on the same column?
(3 answers)
Closed 2 years ago.
I have browsed extensively through SO and havnt found something closely appropriate to what I am looking for.
I have a list of data frames ds. each data frame has 5 columns, A,B,C,D,E,F and I would like to sort the rows in each data frame in this list by columns E and F.
So one data frame might not be sorted as compared to another but the rows in each data frame must be sorted in ascending order. columns E and F are both integer values.
I have tried to use the order() function
ds[with(ds, order(ds$E, ds$F)),]
but it gives an error argument 1 is not a vector. For sorting data in a dataframe this would work fine but i am getting stuck since the data frames are in a list.
Have also tried iterating but doesnt work (P.S. Please dont laugh, complete beginner here. ). where extkeys is the number of dataframes in the list (22)
for (i in length(extkeys))
{
ds2 <- ds[[i]][order(ds$WEEK,ds$YEAR,)]
}
Can someone suggest any alternative/correct way of doing it?
Newbie to R here and dataset is too complicated. Would appreciate the help :)
To expand my comment above into answer, you can use lapply to apply a sort to each data frame in your list:
lapply(ds, function(x) x[order(x$E, x$F), ])

For loop on multiple lists in object [duplicate]

This question already has answers here:
number of rows each data frame in a list [duplicate]
(2 answers)
Closed 3 years ago.
I have 10 lists stored in the object squared.res$squared that can be called using squared.res$squared[1] (list1) squared.res$squared[2] list2 etc. I would like to get the number of rows for each list in the object.
I tried to convert to data frame and do a loop, but the output is empty. I did not manage to produce a data example.
EDIT: squared.res is a list
for (i in 1:10) {
dim(do.call(rbind.data.frame, squared.res$squared[i]))[1]
}
If squared.res$squared is a list, it seems that
lapply(squared.res$squared, length)
should work. Or with sapply instead of lapply, or dim instead of length as #jogo proposes.

Call dataframe with index in a loop [duplicate]

This question already has answers here:
R - use rbind on multiple variables with similar names
(2 answers)
rbind multiple data sets [duplicate]
(4 answers)
Closed 3 years ago.
I have some data frame data1, data2, data10.
I want to combine all those data frames by rows.
I tried using rbind in a loop but i can't call dataframe correctly in the loop.
tempo<-data.frame()
for(j in 1:10){
tempo<-rbind(tempo,eval(parse(paste0("data",j))))
}
but that doesn't work. R doesn't know the dataframe that i want to call with command eval(parse)
Thank you

how to convert a dataframe to a list [duplicate]

This question already has answers here:
data.frame rows to a list
(12 answers)
Closed 4 years ago.
I have a very small csv file that when I import to R, becomes a dataframe. I would like to make this dataframe a list, but "as.list" only reads the dataframe items to me in list form and does not actually make a change to the data. I need to make a properties csv a list in order to use it to create a community in R. Any suggestions would be appreciated!
Technically, a data frame is a list, with the restriction that each element of the list is of the same size. If you want to split your data frame into a list based on the row, you can use split
df_as_list <- split(df, 1:nrow(df))
This can be fancier too, it can be based on the levels of a factor or character vector:
df_as_list <- split(df, df$identifier)
Either of these will create a list of data frames, with some number of rows from the original data frame assigned to each element of the list.

Resources