gpt4 book ai didi

r - 从 R 中的字符串中提取子字符串和数字

转载 作者:行者123 更新时间:2023-12-04 10:03:05 27 4
gpt4 key购买 nike

我有几个字符串,以下是一些示例。

rfoutputtablep7q10000t20000c100
rfoutputtablep7q1000t20000c100
svmLinear2outputtablep7q20000t20000c100
svmLinear2outputtablep7q5000t20000c100

我想用列制作一个数据框: algorithm , p , q , t , 和 c并从这些字符串中提取值。所以之前的东西 "outputtable"algorithm"p"后面的数字是 p 的值, "q" 之后的数字是 q 的值, 等等。

如何创建此数据框?

最佳答案

仅使用基础 R。

res <- do.call(rbind, strsplit(y, 'outputtable|p|q|t|c'))
res <- as.data.frame(res[, -2])
res[-1] <- lapply(res[-1], function(x) as.numeric(as.character(x)))
names(res) <- c("algorithm", "p", "q", "t", "c")
res
# algorithm p q t c
#1 rf 7 10000 20000 100
#2 rf 7 1000 20000 100
#3 svmLinear2 7 20000 20000 100
#4 svmLinear2 7 5000 20000 100

数据。
y <- scan(text = '"rfoutputtablep7q10000t20000c100"
"rfoutputtablep7q1000t20000c100"
"svmLinear2outputtablep7q20000t20000c100"
"svmLinear2outputtablep7q5000t20000c100"',
what = character())

关于r - 从 R 中的字符串中提取子字符串和数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47417792/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com