【问题标题】:How add a column to a data.frame with name extracted from an existing column in R?如何使用从 R 中的现有列中提取的名称将列添加到 data.frame?
【发布时间】:2020-09-14 18:31:07
【问题描述】:

我有DF data.frame。我想添加另一个column(i.e., call it station_no),它将在Variables columnunderscore 之后的extract number

library(lubridate)
library(tidyverse)

set.seed(123)

DF <- data.frame(Date = seq(as.Date("1979-01-01"), to = as.Date("1979-12-31"), by = "day"),
                 Grid_2 = runif(365,1,10), Grid_20 = runif(365,5,15)) %>% 
      pivot_longer(-Date, names_to = "Variables", values_to = "Values")

期望的输出:

DF_out <- data.frame(Date = c("1979-01-01","1979-01-01"),Variables = c("Grid_2","Grid_20"), 
                     Values = c(0.95,1.3),    Station_no = c(2,20))

【问题讨论】:

    标签: r dataframe tidyverse lubridate columnname


    【解决方案1】:

    简单的选项是parse_number,它返回数字转换值

    library(dplyr)
    DF %>% 
       mutate(Station_no  = readr::parse_number(Variables))
    

    或者使用str_extract(如果我们想按照模式进行)

    library(stringr)
    DF %>%
       mutate(Station_no  = str_extract(Variables, "(?<=_)\\d+"))
    

    或使用base R

    DF$Station_no <-  trimws(DF$Variables, whitespace = '\\D+')
    

    【讨论】:

      【解决方案2】:

      base R 的解决方案是:

      #Code
      DF$Station_no <- sub("^[^_]*_", "", DF$Variables)
      

      输出(一些行):

      # A tibble: 730 x 4
         Date       Variables Values Station_no
         <date>     <chr>      <dbl> <chr>     
       1 1979-01-01 Grid_2      3.59 2         
       2 1979-01-01 Grid_20    12.8  20        
       3 1979-01-02 Grid_2      8.09 2         
       4 1979-01-02 Grid_20     6.93 20        
       5 1979-01-03 Grid_2      4.68 2         
       6 1979-01-03 Grid_20     5.18 20        
       7 1979-01-04 Grid_2      8.95 2         
       8 1979-01-04 Grid_20     9.07 20        
       9 1979-01-05 Grid_2      9.46 2         
      10 1979-01-05 Grid_20     9.83 20        
      # ... with 720 more rows
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-18
        • 2014-02-13
        • 2019-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多