在R中导入和清理文本文件
我有以下格式的文本文件中的数据在R中导入和清理文本文件,r,read.table,R,Read.table,我有以下格式的文本文件中的数据 x y 0 1 1 0.93 2 0.86 3 0.8 4 0.75 (0.00,1.00),(1.00,0.93),(2.00,0.86),(3.00,0.8),(4.00,0.75),(5.00,0.7),(6.00,0.65),(7.00,0.6),(8.00,0.56),(9.00,0.52),(10.0,0.49) 括号内的第一个元素是x,第二个元素是y。如何创建具有以
x y
0 1
1 0.93
2 0.86
3 0.8
4 0.75
(0.00,1.00),(1.00,0.93),(2.00,0.86),(3.00,0.8),(4.00,0.75),(5.00,0.7),(6.00,0.65),(7.00,0.6),(8.00,0.56),(9.00,0.52),(10.0,0.49)
括号内的第一个元素是x,第二个元素是y。如何创建具有以下格式的矩阵
x y
0 1
1 0.93
2 0.86
3 0.8
4 0.75
我尝试了read.table的变体,但没有成功。非常感谢您的帮助。像这样的东西怎么样
text <-
"(0.00, 1.00), (1.00, 0.93), (2.00, 0.86), (3.00, 0.8), (4.00, 0.75), (5.00, 0.7), (6.00, 0.65), (7.00, 0.6), (8.00, 0.56), (9.00, 0.52), (10.0, 0.49)";
do.call(rbind, lapply(
gsub("(\\s*\\(|\\)\\s*)", "", unlist(strsplit(text, split = "),"))),
function(x) as.numeric(unlist(strsplit(x, ", ")))))
# [,1] [,2]
# [1,] 0 1.00
# [2,] 1 0.93
# [3,] 2 0.86
# [4,] 3 0.80
# [5,] 4 0.75
# [6,] 5 0.70
# [7,] 6 0.65
# [8,] 7 0.60
# [9,] 8 0.56
#[10,] 9 0.52
#[11,] 10 0.49
首先,我们将字符串保存为文件,以便解决方案可复制:
str0 <- "(0.00, 1.00), (1.00, 0.93), (2.00, 0.86), (3.00, 0.8), (4.00, 0.75), (5.00, 0.7), (6.00, 0.65), (7.00, 0.6), (8.00, 0.56), (9.00, 0.52), (10.0, 0.49)"
file1 <- "str1.xt"
write(str0,file1)
str0取决于实际数据与示例matrix(scan(text=gsub('[()]','',text),sep=','),ncol=2L,byrow=TRUE)的接近程度它没有换行符吗?并且没有标题行?它类似于CSV(除了可能没有换行符)。因此,在括号中插入gsub()
,然后读取.table(…,col.names=c('x','y'))
x <- scan(file1, character())
x <- as.numeric(gsub("[(,)]","",x))
x <- matrix(x,ncol = 2,byrow = TRUE,dimnames = list(NULL,c("x","y")))
as.data.frame(x)
# x y
# 1 0 1.00
# 2 1 0.93
# 3 2 0.86
# 4 3 0.80
# 5 4 0.75
# 6 5 0.70
# 7 6 0.65
# 8 7 0.60
# 9 8 0.56
# 10 9 0.52
# 11 10 0.49
x <- gsub("), (",", ",scan(file1, character(),sep="_"),fixed = T)
as.data.frame(eval(parse(text=paste0("matrix(c",x,",ncol = 2,byrow = TRUE,dimnames = list(NULL,c('x','y')))"))))