简体   繁体   English

如何使用从 R 中的现有列中提取的名称向 data.frame 添加列?

[英]How add a column to a data.frame with name extracted from an existing column in R?

I have the DF data.frame .我有DF data.frame I would like to add another column (ie, call it station_no) where it will extrac t the number after underscore from the Variables column .我想补充另一column (ie, call it station_no)它会extrac t时的numberunderscoreVariables column

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")

Desired Output:期望输出:

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))

Easy option is parse_number which returns numeric converted value简单的选项是parse_number ,它返回数字转换值

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

Or using str_extract (in case we want to go by the pattern)或者使用str_extract (以防我们想按照模式进行)

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

Or using base R或使用base R

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

A base R solution would be: base R解决方案是:

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

Output (some rows):输出(某些行):

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 r 中的嵌套列表 data.frame 中添加列表名称作为列 - How to add a list name as a column at a nested list data.frame in r 如何在 R Data.frame 中使用 if/then 改变列 - How to mutate a column with if/then in R Data.frame 如何将data.frame中的列从POSIXct转换为R中的日期 - How to convert a column in a data.frame from POSIXct to date in R R如何使用另一个data.frame中的值更新data.frame中的列 - R How to update a column in data.frame using values from another data.frame 使用R检索data.frame中具有序列名称的列 - Using R to retrieve the column in data.frame with column name in the sequence 如何在现有data.frame中添加其他列,这些列在data.frame中已有的一个特定列上对齐? - How can I add additional columns to an existing data.frame, that are aligned on one specific column already in the data.frame? R 将列添加到 data.frame 中,即在 data.frames 列表中 - R Add column into data.frame, that is in list of data.frames 现有的使用变量的新data.frame()不会继承列名 - new data.frame() from existing using a variable doesn't carry over column name 使用来自其他 data.frame 列的值填充 data.frame 列,条件为 R - Fill a data.frame column with values from other data.frame column with a condition R R中的data.frame()中的参数用于停止自动更改列名 - Arguments in data.frame() in R for stopping automatic column name changing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM