Save in a for loop all variables even with NA [duplicate] - r

This question already has answers here:
Combine two data frames by rows (rbind) when they have different sets of columns
(14 answers)
Closed 1 year ago.
I try to run in a loop some api call
I have a dataframe which saves in every iteration all data
However there are some iterations which don't have a specific column
Is there any easy way to save it with an NA without needing to know in every iteration which variable doesn't exist
This is what I use to save the data:
dfall <- rbind(dfall, dfiteration)

Use dplyr::bind_rows which will automatically add NA for columns which are not present.
dfall <- dplyr::bind_rows(dfall, dfiteration)

We can use rbindlist from data.table
library(data.table)
dfall <- rbindlist(list(dfall, dfiteration), fill = TRUE)

Related

How can I simplify the following R code into a FOR LOOP? [duplicate]

This question already has answers here:
Split data.frame based on levels of a factor into new data.frames
(3 answers)
Create multiple data frames from one based off values with a for loop
(2 answers)
Closed last year.
I would like to convert this code into FOR LOOP but I met mistakes and I could not finish it. Thank you for your help!
PM2.52018 <- data_df[which(data_df$type=='PM2.5'),]
PM102018<- data_df[which(data_df$type=='PM10'),]
SO2 <- data_df[which(data_df$type=='SO2'),]
CO <- data_df[which(data_df$type=='CO'),]
NO2 <- data_df[which(data_df$type=='NO2'),]
O3 <- data_df[which(data_df$type=='O3'),]
We do not need a for loop here.
Just use split. After that, we can rename the individual data.frames as desired.
This will get us a list of dataframes, which is usually much better than keeping all this likely related dataframes in the global environment
my_list <- split(data_df, data_df$type)

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)

How to make a large list in R without using a loop [duplicate]

This question already has answers here:
How do I make a list of data frames?
(10 answers)
Closed 4 years ago.
I have around 500 dataframes with two columns and a varying number of rows (from 10 till 30) in R. I also have a character vector containing the names for the dataframes. I now wish to bundle all these dataframes into a single list so I can refer to these list elements using the dataframe names.
I was thinking to use a loop to loop through the dataframes and add them to the list in each step but I couldn't pull it of.
The following works:
df_list = mget(df_names)
However, the real solution is to not have 500 data.frames in your workspace to begin with: either load/create them immediately inside a list, or have your data in such a format that it’s collated into one big data.frame.
Think of variables as things you have to keep in your head at the same time: if there are too many to remember, you have too many. Because how else can you reason about the state of your program, i.e. about its semantics and correctness?
Check this:
df_names <- c( ) # yours dataframes names character vector
lst <- list()
for (i in df_names) {
lst[i] <- list(get(i))
}

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.

how can I rbind data sets which have sequence name? [duplicate]

This question already has answers here:
Combine several data frames in the global environment by row (rbind)
(2 answers)
Closed 7 years ago.
I have a problem with rbind data sets.
For example, data set names are like this:
data_1, data_2, data_3,...,data_100
data set number is not fixed. Sometimes 100, sometimes 250.
My method is just write every data sets (a <- rbind(data_1, data_2, ... , data_100).
I want to make function for use loop but I couldn't make it.
We can use pattern argument in ls to get the object names that matches the pattern as a string, then we use mget to get the values in a list and finally rbind the list elements with do.call.
lst <- mget(ls(pattern='data_\\d+'))
do.call(rbind, list)
Or we can use rbindlist
library(data.table)
rbindlist(lst)
Or bind_rows from dplyr
library(dplyr)
bind_rows(lst)

Resources