【问题标题】:Map differing length variables to one data frame将不同长度的变量映射到一个数据帧
【发布时间】:2018-05-25 14:05:38
【问题描述】:

假设我必须关注数据

specialty <- c("Primary Care", "Internal Medicine Subspecialties" , 
 "Pediatric subspecialties","Surgical subspecialties", "Emergency 
  Medicine","All other specialties", "No Medical specialty")


 test <- c(23,43,67,77,54)

dfTEST <- data.frame(test)
dfTEST<- t(dfTEST)
colnames(dfTEST) <- c(1,2,4,5,7)

> dfTEST
      1  2  4  5  7
 test 23 43 67 77 54

请注意,我的 dfTest 有 5 个跳过数字的变量。我需要创建一个数据框,将这些 colname 数字 (1,2,4,5,7) 映射到专业。 Specialty 是与 dfTest colnames 协调的 7 个字符串。意思是dfTest 2 =“内科医学亚专业”和dfTest 4 =“外科亚专科等等。下面是我想要实现的目标的sn-p,但我不知道如何去做。我需要它灵活,因此无论列名中的数字是什么,代码仍然可以工作。有什么想法吗??谢谢!!

> dfTEST
          1                2           4  5  7
 test     23              43           67 77 54
added "primary care"   "internal" ... 

【问题讨论】:

    标签: r dataframe join


    【解决方案1】:

    这应该可以解决您的问题。

    library(dplyr)
    specialty_lookup <- data.frame(specialty = c("Primary Care",
                             "Internal Medicine Subspecialties", 
                             "Pediatric subspecialties",
                             "Surgical subspecialties",
                             "Emergency Medicine",
                             "All other specialties",
                             "No Medical specialty"),
               test = 1:7, 
               stringsAsFactors = F)
    
    data  <-  data.frame(code = c(23,43,67,77,54),
                      test = c(1,2,4,5,7))
    
    data <- data %>% 
      left_join(specialty_lookup)
    
    data_wide <- data %>% 
      select(-test) %>%
      t() %>% 
      data.frame()
    
    colnames(data_wide) <- data$test
    data_wide
    

    但您应该质疑自己是否真的是您希望数据具有的格式。从我对您的问题的了解来看,以下格式会更合适:

    library(dplyr)
    specialty_lookup <- data.frame(specialty = c("Primary Care",
                             "Internal Medicine Subspecialties", 
                             "Pediatric subspecialties",
                             "Surgical subspecialties",
                             "Emergency Medicine",
                             "All other specialties",
                             "No Medical specialty"),
               test = 1:7, stringsAsFactors = F)
    
    data  <-  data.frame(code = c(23,43,67,77,54),
                      test = c(1,2,4,5,7))
    
    data <- data %>% 
      left_join(specialty_lookup)
    
    data
    

    【讨论】:

      【解决方案2】:

      希望这会有所帮助:

      # get the indexes of correspondent specialties
      ids <- as.integer(colnames(dfTEST))
      dfTEST<- as.data.frame(t(dfTEST))
      dfTEST$added <- specialty[ids]
      dfTEST<- t(dfTEST)
      

      输出:

      > dfTEST
            1              2                                  4                        
      test  "23"           "43"                               "67"                     
      added "Primary Care" "Internal Medicine Subspecialties" "Surgical subspecialties"
            5                                     7                     
      test  "77"                                  "54"                  
      added "Emergency \n               Medicine" "No Medical specialty"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-09
        • 2017-04-06
        • 1970-01-01
        • 2022-01-24
        • 1970-01-01
        • 2023-03-21
        • 2019-10-17
        相关资源
        最近更新 更多