簡體   English   中英

匹配不同文件中不同大小的 2 列的值,並返回其中一個文件的第三列的相應值

[英]matching values of 2 columns of different sizes in different files and returning corresponding value of third column of one of those files

我有一個帶有概述文件的工作簿,其中包含有關每個試驗的信息


    Trial   | date| time| call| social status
    Trial001 xxx   xxx    y     Single alone
    Trial002 xxx   xxx    n     pair with partner
    Trial003 xxx   xxx    n     pair with partner

和一個工作簿,其中包含一個文件,其中包含每次試驗中每個人(按行)的不同行為。 (試驗之間的不同科目,但由於 BORIS 都稱為 F1)


    Subject | Trial    | behavior | total count | mean duration (S) | stdv 
    F1      | Trial001 | jump     | 1           | 5                 | stdv
    F1      | Trial001 | walk     | 2           | 10                | stdv
    F1      | Trial002 | jump     | 3           | 10                | stdv
    F1      | Trial002 | walk     | 3           | 10                | stdv
    F1      | Trial002 | no view  | 3           | 50                | stdv

現在我想在行為文件中創建一個列,其中包含相應概述試驗中提到的社會地位。

我以為left_join會解決我的問題,但是當我嘗試這個時,它給我一個錯誤,說它需要~name或它將我的整個變異連接代碼返回到我的控制台而不是填充列。

在這里,我用連接的結果覆蓋了 data.frame behavior

behavior <- behavior %>%
  left_join(overview %>% select(Trial, social_status),
            by="Trial")

Output:

  Subject Trial    behavior total_count mean_duration stdv  social_status    
  <chr>   <chr>    <chr>          <dbl>         <dbl> <chr> <chr>            
1 F1      Trial001 jump               1             5 stdv  Single alone     
2 F1      Trial001 walk               2            10 stdv  Single alone     
3 F1      Trial002 jump               3            10 stdv  pair with partner
4 F1      Trial002 walk               3            10 stdv  pair with partner
5 F1      Trial002 no view            3            50 stdv  pair with partner

輸入:

# Behavior
structure(list(Subject = c("F1", "F1", "F1", "F1", "F1"), Trial = c("Trial001", 
"Trial001", "Trial002", "Trial002", "Trial002"), behavior = c("jump", 
"walk", "jump", "walk", "no view"), total_count = c(1, 2, 3, 
3, 3), mean_duration = c(5, 10, 10, 10, 50), stdv = c("stdv", 
"stdv", "stdv", "stdv", "stdv"), social_status = c("Single alone", 
"Single alone", "pair with partner", "pair with partner", "pair with partner"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-5L))

# overview
structure(list(Trial = c("Trial001", "Trial002", "Trial003"), 
    date = c("xxx", "xxx", "xxx"), time = c("xxx", "xxx", "xxx"
    ), call = c("y", "n", "n"), social_status = c("Single alone", 
    "pair with partner", "pair with partner")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -3L))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM