有办法将两个变异的数据框合并在一起吗?

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

Is there a way to merge two mutated dataframes together?

问题

所以,我基本上有两个数据框,从一个名为'original_data'的单一数据框突变而来:

我在'original_data'上使用了mutate函数,以保留仅<= 600秒的值,所有其他值将返回为NA,这存储在'new_data_one'中。

然后我再次在'original_data'上使用mutate函数,以保留单个值> 600,所有其他值将返回为NA,这存储在'new_data_two'中。

这两个数据框都保留了它们的结构,所以类似这样:

'original_data'

   1  2   3   4   5
R1 80 98 150 599 650
R2 90 95 254 725 905
R3 93 96 540 650 780

'new_data_one'

   1  2   3   4   5
R1 80 98 150 599 NA
R2 90 95 254 NA  NA
R3 93 96 540 NA  NA

'new_data_two'

   1  2   3  4  5
R1 NA NA NA NA  650
R2 NA NA NA 725  NA
R3 NA NA NA 650  NA

我如何合并这两个突变的数据框('new_data_one'和'new_data_two'),以便获得类似下面的输出(基本上保留相同的列/行结构)?

'desired_merged_dataframe'

   1  2   3   4   5
R1 80 98 150 599 650
R2 90 95 254 725 NA
R3 93 96 540 650 NA
英文:

so I basically have two dataframes mutated from a single dataframe 'original_data':

I used the mutate function on 'original data' to keep only values <= 600 seconds, all the other values would return as NA, this is stored in 'new_data_one'.

I then used the mutate function on 'original data' again to keep a single value >600, all the other values would return as NA, this is stored in 'new_data_two'.

Both of these dataframe retained their structure, so something like this:

'original_data'

   1  2   3   4   5
R1 80 98 150 599 650
R2 90 95 254 725 905
R3 93 96 540 650 780

'new_data_one'

   1  2   3   4   5
R1 80 98 150 599 NA
R2 90 95 254 NA  NA
R3 93 96 540 NA  NA

'new_data_two'

   1  2   3  4  5
R1 NA NA NA NA  650
R2 NA NA NA 725  NA
R3 NA NA NA 650  NA

How do I merge the two mutated dataframes ('new_data_one' and 'new_data_two'), so I get an output of something like this (that essentially retains the same col/row structures)?

'desired_merged_dataframe'

   1  2   3   4   5
R1 80 98 150 599 650
R2 90 95 254 725 NA
R3 93 96 540 650 NA

答案1

得分: 4

replace(df1, is.na(df1), df2[is.na(df1)])

       X1 X2  X3  X4  X5
    R1 80 98 150 599 650
    R2 90 95 254 725  NA
    R3 93 96 540 650  NA
library(tidyverse)
map2_df(df1, df2, coalesce)

# 一个 tibble: 3 × 5
     X1    X2    X3    X4    X5
  <int> <int> <int> <int> <int>
1    80    98   150   599   650
2    90    95   254   725    NA
3    93    96   540   650    NA
英文:
replace(df1, is.na(df1), df2[is.na(df1)])

   X1 X2  X3  X4  X5
R1 80 98 150 599 650
R2 90 95 254 725  NA
R3 93 96 540 650  NA

library(tidyverse)
map2_df(df1, df2, coalesce)

# A tibble: 3 &#215; 5
     X1    X2    X3    X4    X5
  &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
1    80    98   150   599   650
2    90    95   254   725    NA
3    93    96   540   650    NA

答案2

得分: 0

这里是一个dplyr的解决方案:

library(dplyr)

new_data_one %>%
  mutate(across(everything(), ~ coalesce(., new_data_two[[cur_column()]])))
  
  X1 X2  X3  X4  X5
R1 80 98 150 599 650
R2 90 95 254 725  NA
R3 93 96 540 650  NA
英文:

Here is a dplyr solution:

library(dplyr)

new_data_one %&gt;%
  mutate(across(everything(), ~ coalesce(., new_data_two[[cur_column()]])))

  X1 X2  X3  X4  X5
R1 80 98 150 599 650
R2 90 95 254 725  NA
R3 93 96 540 650  NA

huangapple
  • 本文由 发表于 2023年6月2日 06:48:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386162.html
匿名

发表评论

匿名网友

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

确定