Here is another approach not using for loop, but using tidyverse tools.
These will allow you to continue processing further.
I took the example back for previous answer
df = read.table(text =
"a,b,c
1,2,3
4,5,6",
header = TRUE,
sep = ",")
for(i in 1:3){
write.csv(df, file.path(tempdir(), paste0("test", i, ".csv")), row.names = FALSE)
}
Using tidyverse
library(tidyverse)
#> Warning: le package 'purrr' a été compilé avec la version R 3.5.2
# get the file path
DATEIEN <- list.files(tempdir(), pattern = "test\\d\\.csv$", full.names = TRUE)
# use a name list for row bind later
DATEIEN <- set_names(DATEIEN, paste0("DATEN", 1:3))
DATEN <- DATEIEN %>%
# read in loop and row bind
map_dfr(read_csv, .id = 'dataset')
#> Parsed with column specification:
#> cols(
#> a = col_double(),
#> b = col_double(),
#> c = col_double()
#> )
#> Parsed with column specification:
#> cols(
#> a = col_double(),
#> b = col_double(),
#> c = col_double()
#> )
#> Parsed with column specification:
#> cols(
#> a = col_double(),
#> b = col_double(),
#> c = col_double()
#> )
DATEN
#> # A tibble: 6 x 4
#> dataset a b c
#> <chr> <dbl> <dbl> <dbl>
#> 1 DATEN1 1 2 3
#> 2 DATEN1 4 5 6
#> 3 DATEN2 1 2 3
#> 4 DATEN2 4 5 6
#> 5 DATEN3 1 2 3
#> 6 DATEN3 4 5 6
you’ll then be able to apply some operation, by dataset using group_by
# sum of all column for each DATENi
DATEN %>%
group_by(dataset) %>%
summarise_all(sum)
#> # A tibble: 3 x 4
#> dataset a b c
#> <chr> <dbl> <dbl> <dbl>
#> 1 DATEN1 5 7 9
#> 2 DATEN2 5 7 9
#> 3 DATEN3 5 7 9
# remove c, and do b - a
DATEN %>%
select(-c) %>%
mutate(d = b - a)
#> # A tibble: 6 x 4
#> dataset a b d
#> <chr> <dbl> <dbl> <dbl>
#> 1 DATEN1 1 2 1
#> 2 DATEN1 4 5 1
#> 3 DATEN2 1 2 1
#> 4 DATEN2 4 5 1
#> 5 DATEN3 1 2 1
#> 6 DATEN3 4 5 1
you also can continue using list and purrr
, i.e not row bind to a data.frame
DATEN_list <- DATEIEN %>%
map(read_csv, col_types = "iii")
str(DATEN_list, give.attr = FALSE)
#> List of 3
#> $ DATEN1:Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 2 obs. of 3 variables:
#> ..$ a: int [1:2] 1 4
#> ..$ b: int [1:2] 2 5
#> ..$ c: int [1:2] 3 6
#> $ DATEN2:Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 2 obs. of 3 variables:
#> ..$ a: int [1:2] 1 4
#> ..$ b: int [1:2] 2 5
#> ..$ c: int [1:2] 3 6
#> $ DATEN3:Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 2 obs. of 3 variables:
#> ..$ a: int [1:2] 1 4
#> ..$ b: int [1:2] 2 5
#> ..$ c: int [1:2] 3 6
# sum of all columns for each DATENi
DATEN_list %>%
map(summarise_all, sum)
#> $DATEN1
#> # A tibble: 1 x 3
#> a b c
#> <int> <int> <int>
#> 1 5 7 9
#>
#> $DATEN2
#> # A tibble: 1 x 3
#> a b c
#> <int> <int> <int>
#> 1 5 7 9
#>
#> $DATEN3
#> # A tibble: 1 x 3
#> a b c
#> <int> <int> <int>
#> 1 5 7 9
# remove c, and do b - a
DATEN_list %>%
map(~ select(.x, - c) %>%
mutate(d = b - a)
)
#> $DATEN1
#> # A tibble: 2 x 3
#> a b d
#> <int> <int> <int>
#> 1 1 2 1
#> 2 4 5 1
#>
#> $DATEN2
#> # A tibble: 2 x 3
#> a b d
#> <int> <int> <int>
#> 1 1 2 1
#> 2 4 5 1
#>
#> $DATEN3
#> # A tibble: 2 x 3
#> a b d
#> <int> <int> <int>
#> 1 1 2 1
#> 2 4 5 1
Created on 2019-01-03 by the reprex package (v0.2.1)