R统计:将不规则文件的子集读入数据帧
我有一个文本文件,它由4个独立的组件组成:源、用法和与数据集关联的实际数据。我想将每个组件读入一个单独的R对象 下面是文件格式的一个示例。每个文件都有关键字SOURCE、STORY、USAGE和DATASET作为分隔符 示例数据集 我的问题只是将USAGE部分作为数据帧读取。我编写了一个快速逐行解析器,它扫描文件中的关键字用法和数据集,并返回它们的行号。但是,该代码在以下情况下起作用:R统计:将不规则文件的子集读入数据帧,r,text,subset,read.table,R,Text,Subset,Read.table,我有一个文本文件,它由4个独立的组件组成:源、用法和与数据集关联的实际数据。我想将每个组件读入一个单独的R对象 下面是文件格式的一个示例。每个文件都有关键字SOURCE、STORY、USAGE和DATASET作为分隔符 示例数据集 我的问题只是将USAGE部分作为数据帧读取。我编写了一个快速逐行解析器,它扫描文件中的关键字用法和数据集,并返回它们的行号。但是,该代码在以下情况下起作用: Usage <- read.table(Output.File, skip= 9, nrows = 6,
Usage <- read.table(Output.File, skip= 9, nrows = 6, header = TRUE)
但这段代码没有
Usage <- read.table(Output.File, skip= Beginrow, nrows = Endrow - Beginr4w, header = TRUE)
如何使read.table或任何其他函数允许使用变量skip和行数?或者,是否有一种更简单的方法将USAGE和DATASET之间的数据作为数据表读入
用法将始终有4列,标题名称与上述文件中的标题名称相同,但用法行数可以从1到任意数字。其思想是,首先您必须设法选择字符串中包含相关数据的所需部分,然后从该子字符串中读取csv。在下面的解决方案中,strsplit函数用于获取USAGE和DATASE之间的部分,不管有多少行。我基本上把绳子分成了方便的几段。您可以通过以下网址了解更多信息:
这里有一个可扩展的方法。首先,用readline将整个文件读入一个变量。我将在这里使用textConnection进行复制,但您应该从文件中读取
x <- readLines(con=textConnection('
SOURCE
Boxofficemojo.com
STORY
These lines, of variable length and number, would contain the story behind the dataset.
USAGE
"Course" "Year" "Section" "Exercise"
"Course1" 5 9 "ex 3"
"Course1" 5 9 "ex 4"
"Course1" 5 9 "ex 5"
"Course2" 5 9 "ex 3"
"Course2" 5 9 "ex 4"
DATASET
Dataset with headers follows.'))
您也可以选择删除尾随的空字符串,可能在呈现数据时,数据中有x2之类的内容,您只需要skip=7I就可以从稍微不同的文件粘贴代码。我最终会在很多文件上运行这个。代码仅仅是为了表明它在硬编码时工作,但在使用变量时失败。好吧,区别必须在其他地方-它不能是变量与硬编码,因为没有这样的区别。如果你正确计算了skip和nrows,那么你的第二个版本就可以了。可能是Endrow中的一个拼写错误-Beginr4w你可能是指BeginrRow而不是Beginr4w-你说你粘贴了它,所以我假设拼写错误在原始版本中。这很有效。我希望我在R方面足够好,能够理解上面75%的内容,而我目前还没有,所以谢谢你!如果要限制标签的实际名称,这将如何工作?也就是说,不是在所有大写字母中查找所有内容,而是查找特定的关键字?x3[namesx3%in%cUSAGE,DATASET]会将其简化为两个。这种方法有点暴力,这意味着如果你的文件很大,那么效率就会下降。如果是这样的话,在R之外做过滤会更好。也许readLinespipesed-ne'/USAGE/,/DATASET/p'ProfessorE.txt就是一个开始。
str <- 'SOURCE
Boxofficemojo.com
STORY
These lines, of variable length and number, would contain the story behind the dataset.
USAGE
"Course" "Year" "Section" "Exercise"
"Course1" 5 9 "ex 3"
"Course1" 5 9 "ex 4"
"Course1" 5 9 "ex 5"
"Course2" 5 9 "ex 3"
"Course2" 5 9 "ex 4"
DATASET
Dataset with headers follows.'
# get the desired part of the string
datasetStr <- strsplit(paste0(strsplit(str, 'USAGE')[[1]][2]), 'DATASET')[[1]][1]
# read it as data frame
df <- read.csv(text = datasetStr, sep = '\t')
> df
Course....Year..Section...Exercise
1 Course1 5 9 ex 3
2 Course1 5 9 ex 4
3 Course1 5 9 ex 5
4 Course2 5 9 ex 3
5 Course2 5 9 ex 4
x <- readLines(con=textConnection('
SOURCE
Boxofficemojo.com
STORY
These lines, of variable length and number, would contain the story behind the dataset.
USAGE
"Course" "Year" "Section" "Exercise"
"Course1" 5 9 "ex 3"
"Course1" 5 9 "ex 4"
"Course1" 5 9 "ex 5"
"Course2" 5 9 "ex 3"
"Course2" 5 9 "ex 4"
DATASET
Dataset with headers follows.'))
head(x)
# [1] ""
# [2] "SOURCE"
# [3] "Boxofficemojo.com"
# [4] ""
# [5] "STORY"
# [6] "These lines, of variable length and number, would contain the story behind the dataset."
allcaps <- grep("^[A-Z]+$", x)
if (allcaps[1] > 1) x <- x[-(1:(allcaps[1]-1))]
str( x2 <- split(x, cumsum(grepl("^[A-Z]+$", x))) )
# List of 4
# $ 1: chr [1:3] "SOURCE" "Boxofficemojo.com" ""
# $ 2: chr [1:3] "STORY" "These lines, of variable length and number, would contain the story behind the dataset." ""
# $ 3: chr [1:8] "USAGE" "\"Course\" \"Year\" \"Section\" \"Exercise\"" "\"Course1\" 5 9 \"ex 3\"" "\"Course1\" 5 9 \"ex 4\"" ...
# $ 4: chr [1:2] "DATASET" "Dataset with headers follows."
str( x3 <- setNames(lapply(x2, `[`, -1L),
sapply(x2, `[`, 1L)) )
# List of 4
# $ SOURCE : chr [1:2] "Boxofficemojo.com" ""
# $ STORY : chr [1:2] "These lines, of variable length and number, would contain the story behind the dataset." ""
# $ USAGE : chr [1:7] "\"Course\" \"Year\" \"Section\" \"Exercise\"" "\"Course1\" 5 9 \"ex 3\"" "\"Course1\" 5 9 \"ex 4\"" "\"Course1\" 5 9 \"ex 5\"" ...
# $ DATASET: chr "Dataset with headers follows."
x3$USAGE <- read.table(textConnection(x3$USAGE), header=TRUE)
str(x3)
# List of 4
# $ SOURCE : chr [1:2] "Boxofficemojo.com" ""
# $ STORY : chr [1:2] "These lines, of variable length and number, would contain the story behind the dataset." ""
# $ USAGE :'data.frame': 5 obs. of 4 variables:
# ..$ Course : Factor w/ 2 levels "Course1","Course2": 1 1 1 2 2
# ..$ Year : int [1:5] 5 5 5 5 5
# ..$ Section : int [1:5] 9 9 9 9 9
# ..$ Exercise: Factor w/ 3 levels "ex 3","ex 4",..: 1 2 3 1 2
# $ DATASET: chr "Dataset with headers follows."