[英]R split a column in this format
I need to split out this column into 2 columns我需要将此列拆分为 2 列
desired outcome is期望的结果是
I have tried strAny but need help as Col 1 is not a fixed with as the date field length varies due to 1 or 2 characters for the day of the month.我已经尝试过 strAny 但需要帮助,因为 Col 1 不是固定的,因为日期字段长度会因当月日期的 1 或 2 个字符而变化。 Any suggestions how to do this?
任何建议如何做到这一点?
We can use separate
with a regex lookaround to split between a digit and a lower case letter我们可以使用
separate
的正则表达式环视来分割数字和小写字母
library(tidyr)
separate(df1, 'col1', into = c('date', 'other'), sep="(?<=[0-9])(?=[A-Za-z])")
# date other
#1 1/1/2000 yogurt
#2 1/1/2000 toilet paper
#3 2/1/2000 soda
#4 11/1/2000 bagels
#5 12/1/2000 fruits
#6 13/1/2000 laundry detergent
Or using base R
with strsplit
或使用带有 strsplit 的
base R
strsplit
do.call(rbind, strsplit(as.character(df1$col1),
"(?<=[0-9])(?=[A-Za-z])", perl = TRUE))
df1 <- structure(list(col1 = c("1/1/2000yogurt", "1/1/2000toilet paper",
"2/1/2000soda", "11/1/2000bagels", "12/1/2000fruits", "13/1/2000laundry detergent"
)), class = "data.frame", row.names = c(NA, -6L))
Here are couple of ways:这里有几种方法:
Using extract
from tidyr
:使用
tidyr
的extract
:
tidyr::extract(df, col1, c('col1', 'col2'), regex = '(.*\\d)(.*)')
Or with dplyr
and stringr
:或使用
dplyr
和stringr
:
library(dplyr)
library(stringr)
df %>%
mutate(col2 = str_extract(col1, '\\d+/\\d+/\\d+'),
col3 = str_remove(col1, col2))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.