how to use for loop to split dataframe (list in list) and assign new name
Use:
dfps = [[1,2,3], [2,3,4], [5,6,7]]
dic_of_dfs = {}
for i, row in enumerate(dfps):
dic_of_dfs.update({f"df_{i}":pd.DataFrame(row)})
dic_of_dfs["df_0"]
output:
Split dataframe into several data frames within a list, each column separately
Try this tidyverse
approach. You can format your data to long to transform columns into rows. Then, with split()
you can create a list based on the column name. Finally, you can apply a function to transform your data to wide at each dataframe in the list and reach the desired output. Here the code:
library(tidyverse)
#Data
df <- data.frame(my_names=sample(LETTERS,4,replace=F),
column2=sample(1.3:100.3,4,replace=T),
column3=sample(1.3:100.3,4,replace=T),
column4=sample(1.3:100.3,4,replace=T),
column5=sample(1.3:100.3,4,replace=T))
#Reshape to long
df2 <- df %>% pivot_longer(cols = -1)
#Split into a list
List <- split(df2,df2$name)
#Now reshape function for wide format
List2 <- lapply(List,function(x){x<-pivot_wider(x,names_from = name,values_from = value);return(x)})
names(List2) <- paste0('df',1:length(List2))
Output:
List2
$df1
# A tibble: 4 x 2
my_names column2
<fct> <dbl>
1 N 21.3
2 H 35.3
3 X 42.3
4 U 89.3
$df2
# A tibble: 4 x 2
my_names column3
<fct> <dbl>
1 N 94.3
2 H 54.3
3 X 2.3
4 U 38.3
$df3
# A tibble: 4 x 2
my_names column4
<fct> <dbl>
1 N 75.3
2 H 94.3
3 X 87.3
4 U 100.
$df4
# A tibble: 4 x 2
my_names column5
<fct> <dbl>
1 N 60.3
2 H 88.3
3 X 14.3
4 U 99.3
R for loop: creating data frames using split?
It is not recommended to create separate dataframes in the global environment, they are difficult to keep track of. Put them in a list instead. You have started off well by using split
and creating list of dataframes. You can then iterate over each dataframe in the list and apply the function on each one of them.
Using by
this would look like as :
by(tss, tss$created_at, function(x) {
bscore3 <- score.sentiment(x$cleaned_text,pos.words,neg.words,.progress='text')
score3 <- as.integer(bscore3$score[[1]])
return(score3)
}) -> result
result
Group dataframe by ID and then split it into multiple dataframes for each group
Creating the data frame:
ID = c("A", "B", "C", "A", "B", "C", "A", "B", "C")
Date = c("01/01/2022", "01/02/2022", "01/03/2022", "01/01/2022", "01/02/2022", "01/03/2022", "01/01/2022", "01/02/2022", "01/03/2022")
Value = c("45", "24", "33", "65", "24", "87", "51", "32", "72")
df <- data.frame(ID,Date,Value)
Splitting the data:
df_a <- df %>%
filter(ID =="A")
df_b <- df %>%
filter(ID =="B")
df_c <- df %>%
filter(ID =="C")
Printing the data:
Now just run the split data frames below:
df_a
df_b
df_c
This will give you the following output:
ID Date Value
1 A 01/01/2022 45
2 A 01/01/2022 65
3 A 01/01/2022 51
ID Date Value
1 B 01/02/2022 24
2 B 01/02/2022 24
3 B 01/02/2022 32
ID Date Value
1 C 01/03/2022 33
2 C 01/03/2022 87
3 C 01/03/2022 72
R function to split a dataframe into multiple dataframe based on their index
We can use slide
library(slider)
library(dplyr)
library(purrr)
slide(seq_len(nrow(df)), .f = ~ .x, .after = 9) %>%
keep(~ length(.x) == 10) %>%
map(~ df %>%
slice(.x))
-testing
> slide(1:15, .f = ~ .x, .after = 9) %>%
keep(~length(.x) == 10)
[[1]]
[1] 1 2 3 4 5 6 7 8 9 10
[[2]]
[1] 2 3 4 5 6 7 8 9 10 11
[[3]]
[1] 3 4 5 6 7 8 9 10 11 12
[[4]]
[1] 4 5 6 7 8 9 10 11 12 13
[[5]]
[1] 5 6 7 8 9 10 11 12 13 14
[[6]]
[1] 6 7 8 9 10 11 12 13 14 15
and with mtcars
dataset
slide(1:15, .f = ~ .x, .after = 9) %>%
keep(~ length(.x) == 10) %>%
map(~ mtcars %>% slice(.x))
[[1]]
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
[[2]]
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
[[3]]
mpg cyl disp hp drat wt qsec vs am gear carb
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
[[4]]
mpg cyl disp hp drat wt qsec vs am gear carb
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
[[5]]
mpg cyl disp hp drat wt qsec vs am gear carb
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.44 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18.00 0 0 3 3
[[6]]
mpg cyl disp hp drat wt qsec vs am gear carb
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.44 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.25 17.98 0 0 3 4
Or using base R
with embed
lapply(asplit(embed(seq_len(nrow(df)), 10)[, 10:1], 1)
function(x) df[x,])
Related Topics
Extract Time (Hms) from Lubridate Date Time Object
Convert Map Data to Data Frame Using Fortify {Ggplot2} for Spatial Objects in R
Efficiently Counting Non-Na Elements in Data.Table
How Does Settimelimit Work in R
R Doesn't Reset the Seed When "L'Ecuyer-Cmrg" Rng Is Used
Naive Bayes in Quanteda VS Caret: Wildly Different Results
Unzip Password Protected Zip Files in R
Highlight Areas Within Certain X Range in Ggplot2
Copying List of Files from One Folder to Other in R
Paste Several Column Values into One Value in R
Pull Nth Day of Month in Xts in R
Percentage of Overlap Between Polygons
Creating Shiny Reactive Variable That Indicates Which Widget Was Last Modified
Continuous Color Bar with Separators Instead of Ticks
How to Control Label Color Depending on Fill Darkness of Bars