Reputation: 1430
When using lapply()
over a vector, each element of the resulting list doesn't have a name, but only an index:
library(dplyr)
vector = c("df1", "df2")
df1 = data.frame(a = rnorm(5), b = rnorm(5, sd = 1.1))
df2 = data.frame(a = rnorm(5), b = rnorm(5, sd = 1.1))
lapply(vector, function(x){
x = get(x) %>%
mutate(c = a+b)
})
#> [[1]]
#> a b c
#> 1 -0.4098768 1.6712810 1.2614041
#> 2 0.7101722 -0.1025184 0.6076538
#> 3 -0.6696859 0.5690932 -0.1005928
#> 4 1.1642214 -0.4660378 0.6981836
#> 5 -0.5158280 1.4574039 0.9415759
#>
#> [[2]]
#> a b c
#> 1 0.91047848 -1.308707 -0.3982281
#> 2 1.87336493 -1.429567 0.4437977
#> 3 0.54171333 1.849589 2.3913028
#> 4 -0.02978158 2.500763 2.4709817
#> 5 1.49926589 1.602463 3.1017284
It does has names when applying over list:
list = list(
df1 = data.frame(a = rnorm(5), b = rnorm(5, sd = 1.1)),
df2 = data.frame(a = rnorm(5), b = rnorm(5, sd = 1.1))
)
lapply(list, function(x){
x = x %>%
mutate(c = a+b)
})
#> $df1
#> a b c
#> 1 0.8228400 -2.5232496 -1.70040963
#> 2 0.3890213 -0.4349408 -0.04591949
#> 3 0.5084719 1.4089123 1.91738415
#> 4 0.2533964 -0.7379615 -0.48456516
#> 5 -0.2474338 1.0520906 0.80465685
#>
#> $df2
#> a b c
#> 1 0.1376350 -1.32304077 -1.1854058
#> 2 0.1314702 1.14775210 1.2792223
#> 3 0.9757047 -1.24806193 -0.2723573
#> 4 -0.5118045 0.09277009 -0.4190344
#> 5 -0.1631715 -0.47573087 -0.6389024
Is there a simple way to use the vector elements as the resulting list names?
Upvotes: 1
Views: 97
Reputation: 887851
We can also use map
library(purrr)
library(dplyr)
map(mget(vector), ~ .x %>%
mutate(c = a + b))
Upvotes: 0
Reputation: 27802
You could name the vector, before lapply
vector = c("df1", "df2")
names(vector) <- vector # <-- here
df1 = data.frame(a = rnorm(5), b = rnorm(5, sd = 1.1))
df2 = data.frame(a = rnorm(5), b = rnorm(5, sd = 1.1))
lapply(vector, function(x){
x = get(x) %>%
mutate(c = a+b)
})
# $df1
# a b c
# 1 -0.36671838 0.8733203 0.5066019
# 2 -0.05029296 -0.8823471 -0.9326401
# 3 0.54252815 -0.2087211 0.3338071
# 4 -1.16789527 0.2598863 -0.9080090
# 5 -0.80664672 -0.4968422 -1.3034889
#
# $df2
# a b c
# 1 0.9042845 1.2978663 2.2021508
# 2 -0.3848533 -0.4563623 -0.8412156
# 3 -1.1681873 1.3224087 0.1542215
# 4 1.4872564 -2.0281272 -0.5408708
# 5 -0.2717229 -0.3780464 -0.6497694
Upvotes: 2
Reputation: 389235
Instead of using get
on each list separately, you can use mget
to get all the dataframes in a list with their names :
lapply(mget(vector), function(x) transform(x, c = a + b))
#$df1
# a b c
#1 -0.60421251 2.4792735 1.8750610
#2 0.06163947 2.0295196 2.0911590
#3 -0.56318825 2.1496891 1.5865009
#4 -2.46292843 1.1641211 -1.2988073
#5 -1.05692446 0.4365812 -0.6203432
#$df2
# a b c
#1 -0.33388039 0.6690467 0.3351663
#2 0.83637236 1.3321715 2.1685439
#3 0.05839826 0.1017032 0.1601015
#4 -0.20686296 0.8667050 0.6598420
#5 0.52682053 0.4629632 0.9897837
Upvotes: 3