根据行号将列/行从一个数据框映射到另一数据框

时间:2018-11-01 16:27:49

标签: r indexing rows

我有两个数据框:

df1<-structure(list(Name = c("sub7", "sub7", "sub7", "sub7", "sub7", 
              "sub7", "sub7", "sub7", "sub7", "sub7"), StimulusName = c("Alpha1", 
               "Alpha1", "Alpha1", "Alpha1", "Alpha1", "Alpha1", "Alpha1", "Alpha1", 
                "Alpha1", "Alpha1"), PupilLeft = c(10.046, 10.05, 10.062, 10.072, 
                10.072, 10.056, 10.056, 10.056, 10.066, 10.066)), row.names = c(NA, 
                -10L), class = c("tbl_df", "tbl", "data.frame"))



    df2<-structure(list(Name = c("sub7", "sub7"), StimulusName = c("Alpha1", 
              "Alpha1"), Row_Num = c(1, 3), Label = c("Onset", "Offset")), row.names = c(NA, 
                -2L), vars = "Name", drop = TRUE, indices = list(0:1), group_sizes = 2L, biggest_group_size = 2L, labels = structure(list(
               Name = "Guilty Subject 07"), row.names = c(NA, -1L), class = "data.frame", vars = "Name", drop = TRUE), class = c("grouped_df", 
               "tbl_df", "tbl", "data.frame"))

我想从Row_Num中的Labeldf2列中获取值,并将它们映射到df1中与{{1 }}。

我想在不使用循环的情况下实现这一目标。也许只是将Row_Num值用作索引?

在这种情况下,最终的数据帧如下所示:

Row_Num

4 个答案:

答案 0 :(得分:1)

另一种选择是在合并之前在Row_Num中创建df1

df_out <- merge(
      transform(df1, Row_Num = seq_len(nrow(df1))),
      df2,
      by = c("Name", "StimulusName", "Row_Num"),
      all.x = TRUE)
df_out$Row_Num <- ifelse(df_out$Row_Num %in% df2$Row_Num, df_out$Row_Num, NA)
df_out
#   Name StimulusName Row_Num PupilLeft  Label
#1  sub7       Alpha1       1    10.046  Onset
#2  sub7       Alpha1      NA    10.050   <NA>
#3  sub7       Alpha1       3    10.062 Offset
#4  sub7       Alpha1      NA    10.072   <NA>
#5  sub7       Alpha1      NA    10.072   <NA>
#6  sub7       Alpha1      NA    10.056   <NA>
#7  sub7       Alpha1      NA    10.056   <NA>
#8  sub7       Alpha1      NA    10.056   <NA>
#9  sub7       Alpha1      NA    10.066   <NA>
#10 sub7       Alpha1      NA    10.066   <NA>

答案 1 :(得分:1)

使用tidyverse

library(tidyverse)
df1 %>%
   mutate(Row_Num = row_number()) %>%
   left_join(df2) %>%
   mutate(Row_Num = replace(Row_Num, !Row_Num %in% c(1, 3), NA))
# A tibble: 10 x 5
#   Name  StimulusName PupilLeft Row_Num Label 
#   <chr> <chr>            <dbl>   <dbl> <chr> 
# 1 sub7  Alpha1            10.0       1 Onset 
# 2 sub7  Alpha1            10.0      NA <NA>  
# 3 sub7  Alpha1            10.1       3 Offset
# 4 sub7  Alpha1            10.1      NA <NA>  
# 5 sub7  Alpha1            10.1      NA <NA>  
# 6 sub7  Alpha1            10.1      NA <NA>  
# 7 sub7  Alpha1            10.1      NA <NA>  
# 8 sub7  Alpha1            10.1      NA <NA>  
# 9 sub7  Alpha1            10.1      NA <NA>  
#10 sub7  Alpha1            10.1      NA <NA>  

如果要通过行名联接

rownames_to_column(df1, "Row_Num") %>% 
    mutate(Row_Num = as.numeric(Row_Num)) %>% 
    left_join(., df2 %>% 
                   ungroup %>% 
                   select(Row_Num, Label), by = "Row_Num") %>%
    mutate(Row_Num = replace(Row_Num, !Row_Num %in% c(1, 3), NA))

或使用match中的base R

i1 <- match(row.names(df1), df2$Row_Num)
df1[names(df2)[3:4]] <- lapply(df2[3:4], `[`, i1)
df1
# A tibble: 10 x 5
#   Name  StimulusName PupilLeft Row_Num Label 
#   <chr> <chr>            <dbl>   <dbl> <chr> 
# 1 sub7  Alpha1            10.0       1 Onset 
# 2 sub7  Alpha1            10.0      NA <NA>  
# 3 sub7  Alpha1            10.1       3 Offset
# 4 sub7  Alpha1            10.1      NA <NA>  
# 5 sub7  Alpha1            10.1      NA <NA>  
# 6 sub7  Alpha1            10.1      NA <NA>  
# 7 sub7  Alpha1            10.1      NA <NA>  
# 8 sub7  Alpha1            10.1      NA <NA>  
# 9 sub7  Alpha1            10.1      NA <NA>  
#10 sub7  Alpha1            10.1      NA <NA>  

答案 2 :(得分:1)

如果我们仅通过Row_Num加入,那么我们可以这样做:

rownames(df2) <- df2$Row_Num
merge(df1, df2, by=0, all.x=TRUE)

   Row.names Name.x StimulusName.x PupilLeft Name.y StimulusName.y Row_Num  Label
1          1   sub7         Alpha1    10.046   sub7         Alpha1       1  Onset
2         10   sub7         Alpha1    10.066   <NA>           <NA>      NA   <NA>
3          2   sub7         Alpha1    10.050   <NA>           <NA>      NA   <NA>
4          3   sub7         Alpha1    10.062   sub7         Alpha1       3 Offset
5          4   sub7         Alpha1    10.072   <NA>           <NA>      NA   <NA>
6          5   sub7         Alpha1    10.072   <NA>           <NA>      NA   <NA>
7          6   sub7         Alpha1    10.056   <NA>           <NA>      NA   <NA>
8          7   sub7         Alpha1    10.056   <NA>           <NA>      NA   <NA>
9          8   sub7         Alpha1    10.056   <NA>           <NA>      NA   <NA>
10         9   sub7         Alpha1    10.066   <NA>           <NA>      NA   <NA>

或者,您可能要使用:

merge(df1, df2, by="row.names", all.x=TRUE)

要使by参数的含义更加隐秘。

答案 3 :(得分:0)

merge怎么样?

df2 <- data.frame(df2, stringsAsFactors = F)
df3 <- merge(df1,df2)
> df3
   Name StimulusName PupilLeft Row_Num  Label
1  sub7       Alpha1    10.046       1  Onset
2  sub7       Alpha1    10.046       3 Offset
3  sub7       Alpha1    10.050       1  Onset
4  sub7       Alpha1    10.050       3 Offset
5  sub7       Alpha1    10.062       1  Onset
6  sub7       Alpha1    10.062       3 Offset
7  sub7       Alpha1    10.072       1  Onset
8  sub7       Alpha1    10.072       3 Offset
9  sub7       Alpha1    10.072       1  Onset
10 sub7       Alpha1    10.072       3 Offset
11 sub7       Alpha1    10.056       1  Onset
12 sub7       Alpha1    10.056       3 Offset
13 sub7       Alpha1    10.056       1  Onset
14 sub7       Alpha1    10.056       3 Offset
15 sub7       Alpha1    10.056       1  Onset
16 sub7       Alpha1    10.056       3 Offset
17 sub7       Alpha1    10.066       1  Onset
18 sub7       Alpha1    10.066       3 Offset
19 sub7       Alpha1    10.066       1  Onset
20 sub7       Alpha1    10.066       3 Offset

编辑:使用建议的方法,将行名作为索引:

df1$id <- 1:nrow(df1)
df2$id <- 1:nrow(df2)
df4 <- merge(df1,df2, by="id", all.x=T)
> df4
   id Name.x StimulusName.x PupilLeft Name.y StimulusName.y Row_Num  Label
1   1   sub7         Alpha1    10.046   sub7         Alpha1       1  Onset
2   2   sub7         Alpha1    10.050   sub7         Alpha1       3 Offset
3   3   sub7         Alpha1    10.062   <NA>           <NA>      NA   <NA>
4   4   sub7         Alpha1    10.072   <NA>           <NA>      NA   <NA>
5   5   sub7         Alpha1    10.072   <NA>           <NA>      NA   <NA>
6   6   sub7         Alpha1    10.056   <NA>           <NA>      NA   <NA>
7   7   sub7         Alpha1    10.056   <NA>           <NA>      NA   <NA>
8   8   sub7         Alpha1    10.056   <NA>           <NA>      NA   <NA>
9   9   sub7         Alpha1    10.066   <NA>           <NA>      NA   <NA>
10 10   sub7         Alpha1    10.066   <NA>           <NA>      NA   <NA>