考虑
text <- "who let the dogs out"
fooo <- strsplit(text, " ")
fooo
[[1]]
[1] "who" "let" "the" "dogs" "out"
Run Code Online (Sandbox Code Playgroud)
输出strsplit是一个列表.然后列表的第一个元素是一个向量,其中包含上面的单词.
为什么函数表现那样?是否有任何情况会返回包含多个元素的列表?
我可以使用
fooo[[1]][1]
[1] "who"
Run Code Online (Sandbox Code Playgroud)
,但有没有更简单的方法?
我无法通过所有可能性破解我的方式knitr,并在RStudio的PDF输出中插入了完整的参考书目,.Rnw脚本和"编译PDF"按钮.PDF中的所需文本将是引用作品的详细信息.
这是一个Lilliputian bibtex文件,名为jabrefbibtest.bib,保存在工作目录中.
@Book{GreentargetEngagement2012,
Title = {"2012 - In - House Counsel New Media Engagement Survey"},
Author = {"Inside Counsel "},
Publisher = {"Greentarget"},
Year = {"2012"},
Pages = {"20"},
Plots = {"9"},
Tables = {"0"},
Url = {"http://www.greentarget.com/wp-content/uploads/2012/01/2012GTZGICSurveyReportFinal-WebsiteVersion.pdf"}
}
@Book{CitiprivateBank,
Title = {"Intellectual Leadership with Law Watch"},
Author = {""},
Publisher = {""},
Year = {"2008"},
Pages = {"2"},
Plots = {"1"},
Tables = {"4"},
Url = {"http://www.citigroup.com/privatebank/lawassociates/pdfs/lawwatch/slipsheet.pdf"}
}
Run Code Online (Sandbox Code Playgroud)
.Rnw剥离的脚本是
\documentclass[11pt]{article}
\usepackage[backend=bibtex]{biblatex}
% \addbibresource{} …Run Code Online (Sandbox Code Playgroud) 使用 XML 包和 XPath 从网站抓取地址,有时我只能得到一个字符串,其中嵌入了我想要的邮政编码。提取邮政编码很简单,但有时会显示其他五位数的字符串。
以下是 df 中问题的一些变体。
zips <- data.frame(id = seq(1, 5), address = c("Company, 18540 Main Ave., City, ST 12345", "Company 18540 Main Ave. City ST 12345-0000", "Company 18540 Main Ave. City State 12345", "Company, 18540 Main Ave., City, ST 12345 USA", "Company, One Main Ave Suite 18540, City, ST 12345"))
Run Code Online (Sandbox Code Playgroud)
下面是提取邮政编码(5 位和 4 位数字)的 R 语句,但它被街道号码和套房号的虚假邮政编码所欺骗(其他地址字符串中可能还有其他可能性)。
regmatches(zips$address, gregexpr("\\d{5}([-]?\\d{4})?", zips$address, perl = TRUE))
Run Code Online (Sandbox Code Playgroud)
对上一个 SO 问题的回答表明“正则表达式将返回最后一个连续的五位数字字符串。它使用负前瞻来确保在返回后没有 5 位数字字符串。”
从地址字符串中提取邮政编码
\b\d{5}\b(?!.*\b\d{5}\b)
Run Code Online (Sandbox Code Playgroud)
但是这个问题和答案涉及 PHP,并提供了一个带有 preg_matches()` 的 if 循环我不熟悉这些语言和工具,但这个想法可能是正确的。
我的问题:什么 …
使用XML我可以刮掉我需要的URL,但是当我使用xpathSApply它时,R会返回不需要的\n和\ t指示符(新行和制表符).这是一个例子:
doc <- htmlTreeParse("http://www.milesstockbridge.com/offices/", useInternal = TRUE) # scrape and parse an HTML site
xpathSApply(doc, "//div[@class='info']//h3", xmlValue)
[1] "\n\t\t\t\t\t\tBaltimore\t\t\t\t\t" "\n\t\t\t\t\t\tCambridge\t\t\t\t\t" "\n\t\t\t\t\t\tEaston\t\t\t\t\t" "\n\t\t\t\t\t\tFrederick\t\t\t\t\t"
[5] "\n\t\t\t\t\t\tRockville\t\t\t\t\t" "\n\t\t\t\t\t\tTowson\t\t\t\t\t" "\n\t\t\t\t\t\tTysons Corner\t\t\t\t\t" "\n\t\t\t\t\t\tWashington\t\t\t\t\t"
Run Code Online (Sandbox Code Playgroud)
正如这个问题所解释的那样,正则表达式函数可以轻松删除不需要的格式元素, 如何删除网站数据收集结果中的\n\t\t\tt?但我宁愿xpath首先做的工作,如果可能的话(我有数百个这样解析).
此外,还有一些功能,例如translate,在这个问题中:
使用Translate函数删除xml中的换行符,但是如何忽略某些标签?以及strip()我在Python问题中看到的内容.我不知道使用R和xpath时哪些可用.
它可能是一个text()函数有帮助,但我不知道如何将它包含在我的xpathSApply表达式中.同样地normalize-space().
如何在角色向量中找到频繁的相邻单词对?例如,使用原油数据集,一些常见的货币对是"原油","石油市场"和"百万桶".
下面的小例子的代码试图识别频繁的术语,然后使用正向前瞻断言,计算频繁术语立即跟随这些频繁术语的次数.但是这次尝试坠毁并烧毁了.
任何指导都将被理解为如何创建在第一列("对")中显示公共对的数据帧以及在第二列("计数")中显示它们在文本中出现的次数.
library(qdap)
library(tm)
# from the crude data set, create a text file from the first three documents, then clean it
text <- c(crude[[1]][1], crude[[2]][1], crude[[3]][1])
text <- tolower(text)
text <- tm::removeNumbers(text)
text <- str_replace_all(text, " ", "") # replace double spaces with single space
text <- str_replace_all(text, pattern = "[[:punct:]]", " ")
text <- removeWords(text, stopwords(kind = "SMART"))
# pick the top 10 individual words by frequency, since they will likely form the most common pairs
freq.terms …Run Code Online (Sandbox Code Playgroud) 假设使用管道分隔符“ firm.pat”将900多个公司名称粘贴在一起以形成正则表达式模式。
firm.pat <- str_c(firms$firm, collapse = "|")
Run Code Online (Sandbox Code Playgroud)
对于一个名为“ bio”的数据框,该数据框具有大的字符变量(每250行包含100多个单词),名为“注释”,我想用空格替换所有公司名称。既有gsub呼叫和str_replace_all呼叫返回相同的神秘的错误。
bio$comment <- gsub(pattern = firm.pat, x = bio$comment, replacement = "")
Error in gsub(pattern = firm.pat, x = bio$comment, replacement = "") :
assertion 'tree->num_tags == num_tags' failed in executing regexp: file 'tre-compile.c', line 634
library(stringr)
bio$comment <- str_replace_all(bio$comment, firm.pat, "")
Error: assertion 'tree->num_tags == num_tags' failed in executing regexp: file 'tre-compile.c', line 634
Run Code Online (Sandbox Code Playgroud)
traceback() 没有启发我。
> traceback()
4: gsub("aaronson rappaport|adams reese|adelson testan|adler pollock|ahlers cooney|ahmuty demers|akerman|akin …Run Code Online (Sandbox Code Playgroud) 由于最终用户的要求很高,我需要了解geom_box绘图上的须线是否可以与盒子本身不同的颜色或类型?
刚刚考虑了带有彩色和虚线的箱线图,我创建了一个最小的示例。
year <- rep("2014", 10)
total <- c(seq(55, 90, 5), 100, 40)
df <- data.frame(year = as.factor(year), total = total)
ggplot(df, aes(x=factor(year), y=total)) +
geom_boxplot(linetype = "dotted", color = "red") +
theme_bw()
Run Code Online (Sandbox Code Playgroud)
下面的图可以有绿色胡须,保留红色框,还是实心胡须,保留虚线框?

这个 SO 问题告诉我们,基础 R 允许大量的晶须线定制。 bxp有几个参数
当您翻转坐标时,如何减少窄条和面板边框之间的空间?使用数据框df和ggplot命令,底栏和刻度线之间有很多空白区域(同样是"供应商"栏上方的宽阔空间).
df <- data.frame(x = c("firm", "vendor"), y = c(50, 20))
ggplot(df, aes(x = x, y = y)) +
geom_bar(stat = "identity", width = 0.4) +
theme_tufte() + coord_flip() +
labs(x = "", y = "")
Run Code Online (Sandbox Code Playgroud)
我尝试scale_x_discrete了两者limits和expand参数都无济于事position = position dodge,同样没有效果.
这个问题提供coord_equal了改变宽高比,从而减少或消除额外空间,但注意到该解决方案不起作用coord_flip.
我有以下字符串表达式,我应用strsplit:
x="Hello I am using stack overflow to ask this question."
y=strsplit(x,"a")
Run Code Online (Sandbox Code Playgroud)
当有一个'a'时,上面的函数会分割x.根据我的理解,返回的向量应该是一个列表,所以说我想得到x的第二个片段,我应该使用:
y[[2]]
Run Code Online (Sandbox Code Playgroud)
但是,这给了我一个错误:
Error in y[[2]] : subscript out of bounds
Run Code Online (Sandbox Code Playgroud)
我不知道如何解决这个问题.我想要的只是访问字符串的碎片段.
我最近一直在尝试data.frame使用该tm包在 R 中的单个列中查找词频。虽然它data.frame本身有许多基于数字和字符的列,但我只对纯文本的单个列感兴趣。虽然我在清理文本本身时没有遇到问题,但一旦我尝试使用findFreqTerms()命令拉取词频,我就会收到以下错误:
Error: inherits(x, c("DocumentTermMatrix", "TermDocumentMatrix")) is not TRUE
Run Code Online (Sandbox Code Playgroud)
我认为这是说我需要将数据转换为 aDocumentTermMatrix或 a TermDocumentMatrix,但是由于我只有一个正在处理的列,因此我也无法创建。错误如下:
> Test <- DocumentTermMatrix(Types)
Error in UseMethod("TermDocumentMatrix", x) :
no applicable method for 'TermDocumentMatrix' applied to an object of class "c('PlainTextDocument', 'TextDocument')"
Run Code Online (Sandbox Code Playgroud)
有没有办法从单列中获取频率计数?我在下面粘贴了我的完整代码,并对我采取的每一步进行了解释。我很感激你们能给我的任何帮助。
> # extracting the single column I wish to analyse from the data frame
Types <-Expenses$Types
> # lower all cases
Types <- tolower(Types)
> # remove punctuation
Types <- removePunctuation(Types)
> …Run Code Online (Sandbox Code Playgroud)