Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 映射和左键将两个列表连接在一起_R - Fatal编程技术网

R 映射和左键将两个列表连接在一起

R 映射和左键将两个列表连接在一起,r,R,我正试图将两个列表合并在一起。我想使用left\u join,因为我知道它,但它不起作用 我已经尝试了以下两种映射,但似乎无法使其工作,我不知道是否可以使用我正在做的事情的方式 map(df2, ~left_join(., df1, by = "station")) map(df2, ~reduce(., df1, by = "station")) df1中的列表1对应于df2中的列表2,这就是我使用map功能的原因。我只想根据站值将它们连接在一起。一个列表的输出如下所示: # A tibbl

我正试图将两个列表合并在一起。我想使用
left\u join
,因为我知道它,但它不起作用

我已经尝试了以下两种映射,但似乎无法使其工作,我不知道是否可以使用我正在做的事情的方式

map(df2, ~left_join(., df1, by = "station"))
map(df2, ~reduce(., df1, by = "station"))
df1
中的列表1对应于
df2
中的列表2,这就是我使用
map
功能的原因。我只想根据
值将它们连接在一起。一个列表的输出如下所示:

# A tibble: 4 x 11
  date         day month  year quarter semester weekday station        value stat  statistic
  <date>     <int> <dbl> <dbl>   <int>    <int>   <dbl> <chr>          <dbl> <chr>     <dbl>
1 2016-01-01     1     1  2016       1        1       1 c_farolillo       17 mean       21.3
2 2016-01-01     1     1  2016       1        1       1 c_farolillo       17 sd         30.0
3 2016-01-01     1     1  2016       1        1       1 plaza_eliptica    25 mean       48.6
4 2016-01-01     1     1  2016       1        1       1 plaza_eliptica    25 sd         47.1
#一个tible:4 x 11
日期日月年季度学期工作日站值统计
1 2016-01-01 1 2016 1 1 c_farolillo 17平均值21.3
2 2016-01-01 1 2016 1 1 c_farolillo 17 sd 30.0
3 2016-01-01 1 2016 1 1 1 eliptica广场25平均48.6
4 2016-01-01 1 2016 1 1 1 eliptica广场25 sd 47.1
数据:


df1代替
map
,我们可以使用
map2
,它将两个对象作为输入,而不是
map

library(purrr)
library(dplyr)
map2(df2, df1, left_join, by = 'station')
#[[1]]
# A tibble: 4 x 11
#  date         day month  year quarter semester weekday station        value stat  statistic
#  <date>     <int> <dbl> <dbl>   <int>    <int>   <dbl> <chr>          <dbl> <chr>     <dbl>
#1 2016-01-01     1     1  2016       1        1       1 c_farolillo       17 mean       21.3
#2 2016-01-01     1     1  2016       1        1       1 c_farolillo       17 sd         30.0
#3 2016-01-01     1     1  2016       1        1       1 plaza_eliptica    25 mean       48.6
#4 2016-01-01     1     1  2016       1        1       1 plaza_eliptica    25 sd         47.1

#[[2]]
#...

谢谢有一种方法我可以使用像这样的东西:
map2(.x=df2,.y=df1,left_-join(.x,.y,by=“station”)
-有时候我想看看数据/变量从哪里来/正在使用。是的,你可以在
left_-join
完美工作之前添加
~
。最后一件事,如果我想在所有的列表中更广泛地使用
pivot\u,现在它已经合并到一个列表中了,我该怎么做呢?我尝试了以下方法:
%>%map(,~pivot\u-witter(names\u-from=stat,values\u-from=statistic))
Nevermind,明白了:
map(,~pivot\u-witter(,names\u-from=statistic,values\u-from=statistic))
一旦我按照您的建议使用
map2
left\u join
加入列表,我想使用
pivot\u wide
使每个列表更宽。
library(purrr)
library(dplyr)
map2(df2, df1, left_join, by = 'station')
#[[1]]
# A tibble: 4 x 11
#  date         day month  year quarter semester weekday station        value stat  statistic
#  <date>     <int> <dbl> <dbl>   <int>    <int>   <dbl> <chr>          <dbl> <chr>     <dbl>
#1 2016-01-01     1     1  2016       1        1       1 c_farolillo       17 mean       21.3
#2 2016-01-01     1     1  2016       1        1       1 c_farolillo       17 sd         30.0
#3 2016-01-01     1     1  2016       1        1       1 plaza_eliptica    25 mean       48.6
#4 2016-01-01     1     1  2016       1        1       1 plaza_eliptica    25 sd         47.1

#[[2]]
#...
Map(merge, df2, df1, MoreArgs = list(by = 'station', all.x = TRUE))