【问题标题】:R dplyr avoid `separate` from dropping the original columnR dplyr 避免“分离”删除原始列
【发布时间】:2022-08-03 04:59:32
【问题描述】:

根据下面的代码和数据,如何避免separate 函数删除原始列(GEOID)?

代码+数据(df):

library(tidyverse)

# Sample data
df = structure(list(GEO_ID = c(\"1500000US120110101021\", \"1500000US120110101022\", 
\"1500000US120110101031\", \"1500000US120110101032\", \"1500000US120110101033\", 
\"1500000US120110101041\"), GEO_ID_REV = c(1.2e+11, 1.2e+11, 1.2e+11, 
1.2e+11, 1.2e+11, 1.2e+11)), row.names = c(NA, -6L), class = c(\"tbl_df\", 
\"tbl\", \"data.frame\"))

# Split GEO_ID into two new columns and keep the existing column as well.
df = df %>% separate(GEO_ID, c(\"GeoID\", \"US\"), \"US\") # Drops the existing GEO_ID column

    标签: r dplyr


    【解决方案1】:

    我们可以使用remove 参数,默认为 TRUE

     df %>% separate(GEO_ID, c("GeoID", "US"), "US", remove = FALSE)
    

    【讨论】:

      猜你喜欢
      • 2016-06-20
      • 2020-03-18
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多