从 R 中的列中删除下划线和前斜杠

标签 r

我希望从下面的列中提取所有数字详细信息

head(df$Session, 5)
[1] "Session_01122016" "Session_02122016" "Session_03122016" "Session_04122016" "Session_05122016"

head(df$Date, 5)
    [1] "01/12/2016" "02/12/2016" "03/12/2016" "04/12/2016" "05/12/2016"

我的预期输出是:

head(df$SessionOutput, 5)
[1] "01122016" "02122016" "03122016" "04122016" "05122016"

head(df$DateOutput, 5)
    [1] "01122016" "02122016" "03122016" "04122016" "05122016"

请问可以这样做吗?

谢谢。

最佳答案

如果每列中的模式都是一致的,您可以简单地使用 gsub() 删除不需要的模式:

df <- data.frame(
  Session = c("Session_01122016","Session_02122016","Session_03122016","Session_04122016","Session_05122016"),
  Date = c("01/12/2016","02/12/2016","03/12/2016","04/12/2016","05/12/2016"),
  stringsAsFactors = F
)

df$SessionOutput <- gsub("Session_", "", df$Session)
df$DateOutput <- gsub("/", "", df$Date, fixed = T)

> head(df$SessionOutput )
[1] "01122016" "02122016" "03122016" "04122016" "05122016"
> head(df$DateOutput )
[1] "01122016" "02122016" "03122016" "04122016" "05122016"

关于从 R 中的列中删除下划线和前斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46138530/

相关文章:

c++ - 从 R 将整个 C++ 管道作为黑盒运行

python - Python 中的 R group_by() + rleid() 等效项

r - Lapply 同时跳过特定列 R

r - 使用列名的字符向量以编程方式对数据框进行排序

r - as.Date(as.POSIXct()) 给出了错误的日期?

regex - 使用正则表达式拆分列中的值

r - 如何按行对数据进行排序

r - 如何将打印(i/j)保存到输出文件?

r - 允许 .SDcols 随 data.table 中的分组变量而变化

r - 如何计算文档中单词与特定术语的接近度