Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将关联规则输出文本文件拆分为列_R - Fatal编程技术网

R 将关联规则输出文本文件拆分为列

R 将关联规则输出文本文件拆分为列,r,R,我有一个保存关联规则的文本文件,所以我使用read.table读取文件,然后将其存储到矩阵中。文本文件数据如下所示 10 <- 8 3 (7,0.318182) 3 <- 8 10 (7,0.4375) 8 <- 3 10 (7,1) 这是我的密码。我需要一些帮助来打破所有这些值,然后将其存储在矩阵中 rules = read.table("C:/Users/Alex/Desktop/rules 1.txt",header = FALSE, quote = "\"", sep

我有一个保存关联规则的文本文件,所以我使用read.table读取文件,然后将其存储到矩阵中。文本文件数据如下所示

10 <- 8 3 (7,0.318182)
3 <- 8 10 (7,0.4375)
8 <- 3 10 (7,1)
这是我的密码。我需要一些帮助来打破所有这些值,然后将其存储在矩阵中

rules = read.table("C:/Users/Alex/Desktop/rules 1.txt",header = FALSE, quote = "\"", sep = ",")
N<-nrow(rules)
trans= subset(rules, select=c("V1"))
foo <- data.frame(do.call('rbind', strsplit(as.character(a),'<-',fixed=TRUE)))
rules=read.table(“C:/Users/Alex/Desktop/rules 1.txt”,header=FALSE,quote=“\”,sep=“,”)

N我不确定我是否完全理解,但如果将
sep
定义为空格,则可以在单独的列中获取前三个数字

> rules = read.table(text = text, header = FALSE, quote = "\"", sep = " ")
> rules
  V1 V2 V3 V4           V5
1 10 <-  8  3 (7,0.318182)
2  3 <-  8 10   (7,0.4375)
3  8 <-  3 10        (7,1)
>rules=read.table(text=text,header=FALSE,quote=“\”,sep=”“)
>规则
V1 V2 V3 V4 V5

110所以我会这样做。假设您将数据保存在一个名为“tmp.txt”的文件中。您可以使用以下命令读取此文件的所有行:

data = readLines("tmp.txt")
这将创建一个数组,该数组的第i个元素上有文本文件的第i行。因此,
data[i]
如下所示:

[1] "10 <- 8 3 (7,0.318182)"
最后,您可以在所有向量上运行
parse_字符串
,并将结果与以下内容绑定在一起:

lapply(data, parse_string) %>% do.call(rbind)
这将为您提供所需的矩阵作为输出:

 [,1] [,2] [,3] [,4]     [,5]
 [1,]   10    8    3    7 0.318182
 [2,]    3    8   10    7 0.437500
 [3,]    8    3   10    7 1.000000

我们可以使用来自
splitstackshape
包的
cSplit
,以及
gsub
来拆分第一列

library(splitstackshape)
df1 <- cSplit(data.frame(a = gsub('<-', '', df$V1)), 'a', ' ', 'wide')
df1
#   a_1 a_2 a_3
#1:  10   8   3
#2:   3   8  10
#3:   8   3  10
数据

dput(df)
structure(list(V1 = c("10 <- 8 3", "3 <- 8 10", "8 <- 3 10"), 
    V2 = c(7L, 7L, 7L), V3 = c(0.318182, 0.4375, 1)), .Names = c("V1", 
"V2", "V3"), row.names = c(NA, -3L), class = "data.frame")
dput(df)

结构(list)(V1=c(“10
read.table(text=text)
也给出了这个结果。我不知道OP从哪里得到了他们看到的东西……因为
splitstackshape
输出是一个data.table,你可以使用
data.table
方法绑定原始数据thi,我已经尝试了代码,但是当我运行
parse\u string(data[1])时
I get
>parse_string(规则[1])[,1][,2][,3][,4][,5][1,]10引入了NA0.318182
NAs。如果我有5个以上的元素,我必须在解析函数中添加更多的元素吗?
lapply(data, parse_string) %>% do.call(rbind)
 [,1] [,2] [,3] [,4]     [,5]
 [1,]   10    8    3    7 0.318182
 [2,]    3    8   10    7 0.437500
 [3,]    8    3   10    7 1.000000
library(splitstackshape)
df1 <- cSplit(data.frame(a = gsub('<-', '', df$V1)), 'a', ' ', 'wide')
df1
#   a_1 a_2 a_3
#1:  10   8   3
#2:   3   8  10
#3:   8   3  10
cbind(df1, df[,-1])
#   a_1 a_2 a_3 V2       V3
#1:  10   8   3  7 0.318182
#2:   3   8  10  7 0.437500
#3:   8   3  10  7 1.000000
dput(df)
structure(list(V1 = c("10 <- 8 3", "3 <- 8 10", "8 <- 3 10"), 
    V2 = c(7L, 7L, 7L), V3 = c(0.318182, 0.4375, 1)), .Names = c("V1", 
"V2", "V3"), row.names = c(NA, -3L), class = "data.frame")