合并两个表 tidyverse

huangapple go评论83阅读模式
英文:

Merging two tables tidyverse

问题

我试图将两个不同的数据框合并在一起,使第二个数据框中的预测变量的值现在附加到第一个数据框中的均值变量的值。我尝试使用full_join,但大多数值返回n/a,除了一个值,然后将表格延伸到自身之下。我对编程和R相对新,所以任何帮助将不胜感激。

我的第一个表格:

# A tibble: 20 × 6
    sd_RT   seRT bits  valid   value  mean
    <dbl>  <dbl> <chr> <chr>   <chr> <dbl>
 1 0.134  0.0291 0.029 invalid 50    0.734
 2 0.106  0.0232 0.029 valid   50    0.611
 3 0.162  0.0353 0.029 invalid 1     0.812
 4 0.143  0.0313 0.029 valid   1     0.675
 5 0.129  0.0282 0.29  invalid 50    0.742
 6 0.0807 0.0181 0.29  valid   50    0.578
 7 0.129  0.0289 0.29  invalid 1     0.798
 8 0.141  0.0308 0.29  valid   1     0.667
 9 0.162  0.0353 0.53  invalid 50    0.798
10 0.100  0.0219 0.53  valid   50    0.590
11 0.162  0.0353 0.53  invalid 1     0.835
12 0.118  0.0257 0.53  valid   1     0.630
13 0.198  0.0433 0.60  invalid 50    0.798
14 0.101  0.0221 0.60  valid   50    0.595
15 0.148  0.0322 0.60  invalid 1     0.854
16 0.120  0.0261 0.60  valid   1     0.646
17 0.174  0.0379 0.86  invalid 50    0.783
18 0.115  0.0251 0.86  valid   50    0.604
19 0.140  0.0314 0.86  invalid 1     0.830
20 0.133  0.0291 0.86  valid   1     0.650

第二个表格:

 predict  mean
     <dbl> <dbl>
 1   0.587 0.604
 2   0.800 0.783
 3   0.646 0.650
 4   0.859 0.851
 5   0.593 0.595
 6   0.782 0.798
 7   0.652 0.646
 8   0.841 0.854
 9   0.594 0.590
10   0.778 0.798
11   0.654 0.630
12   0.837 0.835
13   0.600 0.591
14   0.761 0.742
15   0.660 0.667
16   0.820 0.819
17   0.606 0.611
18   0.744 0.734
19   0.665 0.675
20   0.804 0.812

我正在使用的代码:

temp3 <- full_join(e1RTbits, temp2, by = c("mean", "predict"))

输出:

# A tibble: 39 × 7
     sd_RT    seRT bits  valid   value  mean predict
    <dbl>   <dbl> <chr> <chr>   <chr> <dbl>   <dbl>
 1  0.134   0.0291 0.029 invalid 50    0.734  NA    
 2  0.106   0.0232 0.029 valid   50    0.611  NA    
 3  0.162   0.0353 0.029 invalid 1     0.812  NA    
 4  0.143   0.0313 0.029 valid   1     0.675  NA    
 5  0.129   0.0282 0.29  invalid 50    0.742  NA    
 6  0.0807  0.0181 0.29  valid   50    0.578  NA    
 7  0.129   0.0289 0.29  invalid 1     0.798  NA    
 8  0.141   0.0308 0.29  valid   1     0.667  NA    
 9  0.162   0.0353 0.53  invalid 50    0.798  NA    
10  0.100   0.0219 0.53  valid   50    0.590   0.594
11  0.162   0.0353 0.53  invalid 1     0.835  NA    
12  0.118   0.0257 0.53  valid   1     0.630  NA    
13  0.198   0.0433 0.60  invalid 50    0.798  NA    
14  0.101   0.0221 0.60  valid   50    0.595  NA    
15  0.148   0.0322 0.60  invalid 1     0.854  NA    
16  0.120   0.0261 0.60  valid   1     0.646  NA    
17  0.174   0.0379 0.86  invalid 50    0.783  NA    
18  0.115   0.0251

<details>
<summary>英文:</summary>

I&#39;m attempting to merge two different dataframes together such that the values of the predict variable in the second data frame is now attached to the values of the mean variable in the first data frame. I have attempted to use full_join, however most values returning n/a except for one, and then just extends the table below itself. I am relatively new to coding and R, so any help would be greatly appreciated.



My first table:

A tibble: 20 × 6

sd_RT   seRT bits  valid   value  mean
&lt;dbl&gt;  &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt;   &lt;chr&gt; &lt;dbl&gt;

1 0.134 0.0291 0.029 invalid 50 0.734
2 0.106 0.0232 0.029 valid 50 0.611
3 0.162 0.0353 0.029 invalid 1 0.812
4 0.143 0.0313 0.029 valid 1 0.675
5 0.129 0.0282 0.29 invalid 50 0.742
6 0.0807 0.0181 0.29 valid 50 0.578
7 0.129 0.0289 0.29 invalid 1 0.798
8 0.141 0.0308 0.29 valid 1 0.667
9 0.162 0.0353 0.53 invalid 50 0.798
10 0.100 0.0219 0.53 valid 50 0.590
11 0.162 0.0353 0.53 invalid 1 0.835
12 0.118 0.0257 0.53 valid 1 0.630
13 0.198 0.0433 0.60 invalid 50 0.798
14 0.101 0.0221 0.60 valid 50 0.595
15 0.148 0.0322 0.60 invalid 1 0.854
16 0.120 0.0261 0.60 valid 1 0.646
17 0.174 0.0379 0.86 invalid 50 0.783
18 0.115 0.0251 0.86 valid 50 0.604
19 0.140 0.0314 0.86 invalid 1 0.830
20 0.133 0.0291 0.86 valid 1 0.650

Second table:

predict mean
<dbl> <dbl>
1 0.587 0.604
2 0.800 0.783
3 0.646 0.650
4 0.859 0.851
5 0.593 0.595
6 0.782 0.798
7 0.652 0.646
8 0.841 0.854
9 0.594 0.590
10 0.778 0.798
11 0.654 0.630
12 0.837 0.835
13 0.600 0.591
14 0.761 0.742
15 0.660 0.667
16 0.820 0.819
17 0.606 0.611
18 0.744 0.734
19 0.665 0.675
20 0.804 0.812

Code I&#39;m using:

temp3 <- full_join(e1RTbits, temp2, by = c("mean", "predict"))

Output:

A tibble: 39 × 7

 sd_RT    seRT bits  valid   value  mean predict
&lt;dbl&gt;   &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt;   &lt;chr&gt; &lt;dbl&gt;   &lt;dbl&gt;

1 0.134 0.0291 0.029 invalid 50 0.734 NA
2 0.106 0.0232 0.029 valid 50 0.611 NA
3 0.162 0.0353 0.029 invalid 1 0.812 NA
4 0.143 0.0313 0.029 valid 1 0.675 NA
5 0.129 0.0282 0.29 invalid 50 0.742 NA
6 0.0807 0.0181 0.29 valid 50 0.578 NA
7 0.129 0.0289 0.29 invalid 1 0.798 NA
8 0.141 0.0308 0.29 valid 1 0.667 NA
9 0.162 0.0353 0.53 invalid 50 0.798 NA
10 0.100 0.0219 0.53 valid 50 0.590 0.594
11 0.162 0.0353 0.53 invalid 1 0.835 NA
12 0.118 0.0257 0.53 valid 1 0.630 NA
13 0.198 0.0433 0.60 invalid 50 0.798 NA
14 0.101 0.0221 0.60 valid 50 0.595 NA
15 0.148 0.0322 0.60 invalid 1 0.854 NA
16 0.120 0.0261 0.60 valid 1 0.646 NA
17 0.174 0.0379 0.86 invalid 50 0.783 NA
18 0.115 0.0251 0.86 valid 50 0.604 NA
19 0.140 0.0314 0.86 invalid 1 0.830 NA
20 0.133 0.0291 0.86 valid 1 0.650 NA
21 NA NA NA NA NA 0.604 0.587
22 NA NA NA NA NA 0.783 0.800
23 NA NA NA NA NA 0.650 0.646
24 NA NA NA NA NA 0.851 0.859
25 NA NA NA NA NA 0.595 0.593
26 NA NA NA NA NA 0.798 0.782
27 NA NA NA NA NA 0.646 0.652
28 NA NA NA NA NA 0.854 0.841
29 NA NA NA NA NA 0.798 0.778
30 NA NA NA NA NA 0.630 0.654
31 NA NA NA NA NA 0.835 0.837
32 NA NA NA NA NA 0.591 0.600
33 NA NA NA NA NA 0.742 0.761
34 NA NA NA NA NA 0.667 0.660
35 NA NA NA NA NA 0.819 0.820
36 NA NA NA NA NA 0.611 0.606
37 NA NA NA NA NA 0.734 0.744
38 NA NA NA NA NA 0.675 0.665
39 NA NA NA NA NA 0.812 0.804


</details>
# 答案1
**得分**: 1
你应该将 `full join` 改为 `left join`,并将 `by` 减少为 `&quot;mean&quot;`。
```R
temp3 <- left_join(e1RTbits, temp2, by = &quot;mean&quot;)
英文:

You should change the full join into a left join and reduce by to &quot;mean&quot;.

temp3 &lt;- left_join(e1RTbits, temp2, by = &quot;mean&quot;)

huangapple
  • 本文由 发表于 2023年7月6日 13:33:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定