【问题标题】:how to write R function to find row names into column name and extract value?如何编写 R 函数以将行名查找到列名中并提取值?
【发布时间】:2020-05-20 05:24:48
【问题描述】:

我是 R 的初学者。
我有两个数据框,想要提取行名称与 coll 名称匹配的值。 我可以使用匹配功能吗?

## dataframe 1 has 5 observations and its 5 element    
df = data.frame(x = c("P1","P2","P3","P4","P5"), 
                f_1 = c("NA","1","NA","NA","NA"), 
                f_2= c("NA","1","NA","NA","NA"),
                f_3= c("1","7","NA","NA","NA"),
                f_4= c("NA","NA","5","NA","NA"),
                f_5= c("NA","NA","2","NA","NA"),
                stringsAsFactors = FALSE)

## Dataframe 2 has 5 observations and their allias.
df2 = data.frame(x = c("D1","D2","D3","D4","D5"),
                 f=c("f_1","f_20","f_30","f_4","f_15"))

##我想匹配标题中的行

我需要的输出是: 在新数据框中创建新列。

    x   f   New
    D1  P2  1
    D2  NA  NA
    D3  NA  NA
    D4  P3  5
    D5  NA  NA

【问题讨论】:

  • 对不起,我真的不明白。什么的行名应该匹配什么的列名?
  • 嗨,我想将 df2 中第二列的每一行与 df 的标题相匹配,如果匹配,则将该行值和行名放在单独的 df3 中。

标签: r indexing match


【解决方案1】:
library(tidyverse)

df %>%
  gather(f, New, -x) %>%         # reshape dataset
  filter(New != "NA") %>%        # remove "NA" rows
  right_join(df2, by = "f") %>%  # join to the other dataset
  select(x=x.y, f=x.x, New)      # select and rename columns of interest

#    x    f  New
# 1 D1   P2    1
# 2 D2 <NA> <NA>
# 3 D3 <NA> <NA>
# 4 D4   P3    5
# 5 D5 <NA> <NA>

【讨论】:

    【解决方案2】:

    执行此操作的简单方法是将您的 df 从宽格式重新调整为长格式,以便更轻松地过滤连接数据以获得所需的行。

    library(tidyr)
    library(dplyr)
    #> 
    #> Attaching package: 'dplyr'
    #> The following objects are masked from 'package:stats':
    #> 
    #>     filter, lag
    #> The following objects are masked from 'package:base':
    #> 
    #>     intersect, setdiff, setequal, union
    
    df = data.frame(x = c("P1","P2","P3","P4","P5"), 
                    f_1 = c("NA","1","NA","NA","NA"), 
                    f_2= c("NA","1","NA","NA","NA"),
                    f_3= c("1","7","NA","NA","NA"),
                    f_4= c("NA","NA","5","NA","NA"),
                    f_5= c("NA","NA","2","NA","NA"),
                    stringsAsFactors = FALSE)
    
    df2 = data.frame(x = c("D1","D2","D3","D4","D5"),
                     f=c("f_1","f_20","f_30","f_4","f_15"), 
                     stringsAsFactors = FALSE)
    
    # reshape df from wide to long format
    longdf <- df %>% gather(f, fvalue, -x)
    
    # to the data wrangling to get your required output
    df3 <- df2 %>% 
        left_join(longdf, by = c("f" = "f")) %>% 
        rename(x = x.x, fx = f, f = x.y, New = fvalue) %>% 
        arrange(x, New) %>% 
        group_by(x) %>% 
        slice(1) %>% 
        ungroup() %>% 
        select(-fx)
    
    df3
    #> # A tibble: 5 x 3
    #>   x     f     New  
    #>   <chr> <chr> <chr>
    #> 1 D1    P2    1    
    #> 2 D2    <NA>  <NA> 
    #> 3 D3    <NA>  <NA> 
    #> 4 D4    P3    5    
    #> 5 D5    <NA>  <NA>
    

    reprex package (v0.3.0) 于 2020 年 2 月 4 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 2022-12-18
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多