我希望从下面的列中提取所有数字详细信息
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/