Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.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_Join_Dplyr - Fatal编程技术网

R 在数据帧中连接额外值

R 在数据帧中连接额外值,r,join,dplyr,R,Join,Dplyr,我有这样一个数据帧: df <- tibble(ID = c(1,1,1,2,2,2,3,3,4,5,5,5,5), Time = c(5,1,3,2,8,5,1,7,2,1,2,3,4), Output = c(1,2,3,4,5,6,7,8,9,10,11,12,13)) %>% arrange(ID, Time) > df # A tibble: 13 x 3 ID Time Output

我有这样一个数据帧:

df <- tibble(ID = c(1,1,1,2,2,2,3,3,4,5,5,5,5),
             Time = c(5,1,3,2,8,5,1,7,2,1,2,3,4),
             Output = c(1,2,3,4,5,6,7,8,9,10,11,12,13)) %>%
      arrange(ID, Time)

> df
# A tibble: 13 x 3
      ID  Time Output
   <dbl> <dbl>  <dbl>
 1     1     1      2
 2     1     3      3
 3     1     5      1
 4     2     2      4
 5     2     5      6
 6     2     8      5
 7     3     1      7
 8     3     7      8
 9     4     2      9
10     5     1     10
11     5     2     11
12     5     3     12
13     5     4     13

使用
tidyr::complete

tidyr::complete(df, ID, Time = 1:10, fill = list(Output = 0))

# A tibble: 50 x 3
#      ID  Time Output
#   <dbl> <dbl>  <dbl>
# 1     1     1      2
# 2     1     2      0
# 3     1     3      3
# 4     1     4      0
# 5     1     5      1
# 6     1     6      0
# 7     1     7      0
# 8     1     8      0
# 9     1     9      0
#10     1    10      0
# … with 40 more rows
tidyr::complete(df,ID,Time=1:10,fill=list(Output=0))
#一个tibble:50x3
#ID时间输出
#      
# 1     1     1      2
# 2     1     2      0
# 3     1     3      3
# 4     1     4      0
# 5     1     5      1
# 6     1     6      0
# 7     1     7      0
# 8     1     8      0
# 9     1     9      0
#10     1    10      0
#…还有40行

使用
tidyr::complete

tidyr::complete(df, ID, Time = 1:10, fill = list(Output = 0))

# A tibble: 50 x 3
#      ID  Time Output
#   <dbl> <dbl>  <dbl>
# 1     1     1      2
# 2     1     2      0
# 3     1     3      3
# 4     1     4      0
# 5     1     5      1
# 6     1     6      0
# 7     1     7      0
# 8     1     8      0
# 9     1     9      0
#10     1    10      0
# … with 40 more rows
tidyr::complete(df,ID,Time=1:10,fill=list(Output=0))
#一个tibble:50x3
#ID时间输出
#      
# 1     1     1      2
# 2     1     2      0
# 3     1     3      3
# 4     1     4      0
# 5     1     5      1
# 6     1     6      0
# 7     1     7      0
# 8     1     8      0
# 9     1     9      0
#10     1    10      0
#…还有40行

我们可以使用
展开
左联合

library(dplyr)
library(tidyr)
df %>%
    expand(ID, Time = 1:10) %>% 
    left_join(df)

我们可以使用
expand
left\u-join

library(dplyr)
library(tidyr)
df %>%
    expand(ID, Time = 1:10) %>% 
    left_join(df)