【问题标题】:Creating a new dataframes based on two conditions from two other dataframes根据来自其他两个数据帧的两个条件创建一个新数据帧
【发布时间】:2020-06-23 12:18:34
【问题描述】:

我对编码语言相当陌生,并被要求根据两个现有数据框创建一个新数据框。数据帧 1 是原始数据帧,数据帧 2 是原始数据帧的子集。新数据框需要是原始数据框的副本,如果它们满足某些条件,则删除某些分数 df2,即识别df2中的任务类型,如果样本ID匹配,则从df1中删除对应的值。

例如Dataframe1:

sample_id  Low    Mid  High

13420       NA    2.4  3.7
43905       7.5   NA   NA
52078       5.6   3.2  5.6
43292       10    NA   1.9
79327       5.7   3.2  NA

数据框2:

Sample    Task type

13420       High
52078       Low
52078       Mid
43292       High
79327       Low


New dataframe:

13420       NA    2.4  NA
43905       7.5   NA   NA
52078       NA    NA   5.6
43292       10    NA   NA
79327       NA    3.2  NA

有人可以帮忙吗?我尝试了一些条件语句,但都没有运气。

【问题讨论】:

  • 您同时拥有rpython 标签。请说明您希望使用哪种语言回答。
  • 我会更喜欢 r
  • 你能解释一下过滤条件吗?由于您在 df2 中有 13420=High,但您在 New dataframe 中保留了“中间”值,因此不清楚您将如何过滤
  • df2 被用作选择训练材料的列表,我们想从 df1 中选择一个新列表,但需要过滤掉 df2 中已经使用的值

标签: r dataframe data-wrangling


【解决方案1】:

样本数据

df1 <- data.table::fread("sample_id   Low   Mid  High
13420       NA    2.4  3.7
43905       7.5   NA   NA
52078       5.6   3.2  5.6
43292       10    NA   1.9
79327       5.7   3.2  NA")

df2 <- data.table::fread("Sample      Tasktype
13420       High
52078       Low
52078       Mid
43292       High
79327       Low")

代码

library( data.table )    
#or make data.frames a data.table
data.table::setDT(df1);data.table::setDT(df2)

#melt df1 to long format
df1.melt <- melt( df1, id.vars = "sample_id" )
#update join the molten dataset with df2, updating the value with NA
df1.melt[ df2, value := NA, on = .(sample_id = Sample, variable = Tasktype) ]
#and cast df1 wit the new values back to wide format
dcast( df1.melt, sample_id ~ variable, value.var = "value" )

输出

#    sample_id  Low Mid High
# 1:     13420   NA 2.4   NA
# 2:     43292 10.0  NA   NA
# 3:     43905  7.5  NA   NA
# 4:     52078   NA  NA  5.6
# 5:     79327   NA 3.2   NA

【讨论】:

    【解决方案2】:

    这是dplyrtidyr 的方法:

    library(dplyr)
    library(tidyr)
    df1 %>%
      pivot_longer(-sample_id) %>%
      left_join(df2, by = c("sample_id" = "Sample",
                            "name" = "Task.type"),
                keep = TRUE) %>%
      mutate(value = ifelse(!is.na(Task.type) & Task.type == name,
                            NA_real_, value)) %>%
      dplyr::select(-c(Sample,Task.type)) %>%
      pivot_wider(id_cols = c("sample_id"))
    ## A tibble: 5 x 4
    #  sample_id   Low   Mid  High
    #      <int> <dbl> <dbl> <dbl>
    #1     13420  NA     2.4  NA  
    #2     43905   7.5  NA    NA  
    #3     52078  NA    NA     5.6
    #4     43292  10    NA    NA  
    #5     79327  NA     3.2  NA  
    

    样本数据

    df1<-structure(list(sample_id = c(13420L, 43905L, 52078L, 43292L, 
    79327L), Low = c(NA, 7.5, 5.6, 10, 5.7), Mid = c(2.4, NA, 3.2, 
    NA, 3.2), High = c(3.7, NA, 5.6, 1.9, NA)), class = "data.frame", row.names = c(NA, 
    -5L))
    df2 <- structure(list(Sample = c(13420L, 52078L, 52078L, 43292L, 79327L
    ), Task.type = structure(c(1L, 2L, 3L, 1L, 2L), .Label = c("High", 
    "Low", "Mid"), class = "factor")), class = "data.frame", row.names = c(NA, 
    -5L))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      • 2013-06-14
      • 2020-09-18
      • 1970-01-01
      • 2017-11-10
      相关资源
      最近更新 更多