基于下面的代码和数据,如何避免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发布于 2022-08-02 20:59:32
我们可以使用remove参数,这在默认情况下是正确的
library(dplyr)
library(tidyr)
df %>%
separate(GEO_ID, c("GeoID", "US"), "US", remove = FALSE)-output
# A tibble: 6 × 4
GEO_ID GeoID US GEO_ID_REV
<chr> <chr> <chr> <dbl>
1 1500000US120110101021 1500000 120110101021 120000000000
2 1500000US120110101022 1500000 120110101022 120000000000
3 1500000US120110101031 1500000 120110101031 120000000000
4 1500000US120110101032 1500000 120110101032 120000000000
5 1500000US120110101033 1500000 120110101033 120000000000
6 1500000US120110101041 1500000 120110101041 120000000000https://stackoverflow.com/questions/73213747
复制相似问题