请帮我在R中安装stringr和stringi包.结果是:
install.packages("stringi")
Installing package into ‘C:/Users/kozlovpy/Documents/R/win-library/3.2’
(as ‘lib’ is unspecified)
?????? URL 'https://mran.revolutionanalytics.com/snapshot/2015-08-27/bin/windows/contrib/3.2/stringi_0.5-5.zip'
Error in download.file(url, destfile, method, mode = "wb", ...) :
?? ???? ??????? URL 'https://mran.revolutionanalytics.com/snapshot/2015-08-27/bin/windows/contrib/3.2/stringi_0.5-5.zip'
????????: ??????????????:
? download.file(url, destfile, method, mode = "wb", ...) :
'InternetOpenUrl' ?? ???????: '???????? ???????? ?? ????????'
?????????????? ? download.packages(pkgs, destdir = tmpd, available = available, :
?????????? ?????? ‘stringi’ ?? ???????
sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale: …Run Code Online (Sandbox Code Playgroud) 我用的时候
library(Hmisc)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : there is no package called 'stringi'
Error: package 'ggplot2' could not be loaded
Run Code Online (Sandbox Code Playgroud)
同样,如果我使用
library(ggplot2)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : there is no package called 'stringi'
Error: package or namespace load failed for 'ggplot2'
Run Code Online (Sandbox Code Playgroud)
我试过安装'stringi'install.packages("stringi")
但在某些时候,在安装过程中,我收到以下错误消息:
configure: error: in `/private/var/folders/pr/wdr5dvjj24bb4wwnjpg1hndc0000gr/T/RtmpeQ5pXk/R.INSTALL10b94a012cab/stringi':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
ERROR: configuration failed for package 'stringi'
* removing '/Library/Frameworks/R.framework/Versions/3.2/Resources/library/stringi' …Run Code Online (Sandbox Code Playgroud) 我正在尝试将字符串转换为数字,并遇到了一些意外的行为str_replace.这是一个最小的工作示例:
library(stringr)
x <- c("0", "NULL", "0")
# This works, i.e. 0 NA 0
as.numeric(str_replace(x, "NULL", ""))
# This doesn't, i.e. NA NA NA
as.numeric(str_replace(x, "NULL", NA))
Run Code Online (Sandbox Code Playgroud)
在我看来,第二个例子应该工作,因为它应该只替换向量中的第二个条目NA(它是字符向量中的有效值).但事实并非如此:内部str_replace将所有三个条目转换为NA.
这里发生了什么?我查看了文档str_replace,stri_replace_all但没有看到明显的解释.
编辑:为了澄清,这是stringr_1.0.0和stringi_1.0-1R上3.1.3,Windows 7操作系统.
我正在尝试根据它是否为表单来过滤符号列 \uxxxx
这在视觉上很容易,也就是说,有些看起来像$, ¢, £,而另一些看起来像\u058f, \u060b, \u07fe。
但我似乎无法弄清楚使用stringi/dplyr
library(dplyr)
library(stringi)
df <- structure(list(Character = c("\\u0024", "\\u00A2", "\\u00A3",
"\\u00A4", "\\u00A5", "\\u058F", "\\u060B", "\\u07FE", "\\u07FF",
"\\u09F2", "\\u09F3", "\\u09FB", "\\u0AF1", "\\u0BF9", "\\u0E3F",
"\\u17DB", "\\u20A0", "\\u20A1", "\\u20A2", "\\u20A3"),
Symbol = c("$", "¢", "£", "¤", "¥", "\u058f", "\u060b", "\u07fe", "\u07ff",
"?", "?", "\u09fb", "\u0af1", "\u0bf9", "?", "?", "?",
"?", "?", "?")), row.names = c(NA, 20L), class = "data.frame")
Character Symbol
1 \\u0024 $
2 …Run Code Online (Sandbox Code Playgroud) Karl Broman的帖子:https://kbroman.wordpress.com/2015/06/22/randomized-hobbit-2/ 让我玩正则表达式和ngrams只是为了好玩.我试图用正则表达式提取2克.我知道有解析器可以做到这一点,但我对正则表达式逻辑感兴趣(即,这是一个我无法满足的自我挑战).
下面我给出一个最小的例子和所需的输出.我尝试的问题是2折:
克(单词)被吃掉了,下次通过时无法使用. 如何让它们可用于第二次传递?(例如,我希望在之前已被消费之后like可用)like toastI like
我无法在未捕获的单词之间创建空格(即使我使用过,也请注意输出中的尾随空格(?:\\s*)). 如何在第n个(在本例中为第二个)单词中捕获尾随空格?我知道这可以简单地完成:"(\\b[A-Za-z']+\\s)(\\b[A-Za-z']+)"对于2克但我想将解决方案扩展到n-gram.PS我知道,\\w但我不认为下划线和数字是单词部分,但请考虑'作为单词部分.
MWE:
library(stringi)
x <- "I like toast and jam."
stringi::stri_extract_all_regex(
x,
pattern = "((\\b[A-Za-z']+\\b)(?:\\s*)){2}"
)
## [[1]]
## [1] "I like " "toast and "
Run Code Online (Sandbox Code Playgroud)
期望的输出:
## [[1]]
## [1] "I like" "like toast" "toast and" "and jam"
Run Code Online (Sandbox Code Playgroud) stringi我正在尝试使用以下命令安装一个名为的包
install.packages("stringi")
虽然它没有抛出任何错误消息,但安装尚未结束。
\n我在控制台屏幕上看到很多消息,该屏幕持续运行超过 45 分钟
\n> install.packages("stringi")\nInstalling package into \xe2\x80\x98C:/Users/Aks/Documents/R/win-library/3.6\xe2\x80\x99\n(as \xe2\x80\x98lib\xe2\x80\x99 is unspecified)\n\n There is a binary version available but the source version is later:\n binary source needs_compilation\nstringi 1.4.6 1.5.3 TRUE\n\ninstalling the source package \xe2\x80\x98stringi\xe2\x80\x99\n\ntrying URL 'http://mirror.las.iastate.edu/CRAN/src/contrib/stringi_1.5.3.tar.gz'\nContent type 'application/x-gzip' length 7293930 bytes (7.0 MB)\ndownloaded 7.0 MB\n\n* installing *source* package 'stringi' ...\n** package 'stringi' successfully unpacked and MD5 sums checked\n** using staged installation\n** libs\n\n*** arch - i386\nC:/Rtools/mingw_32/bin/g++ -std=gnu++11 -I"C:/PROGRA~1/R/R-36~1.2/include" -DNDEBUG -I. -Iicu61/ -Iicu61/unicode -Iicu61/common -Iicu61/i18n …Run Code Online (Sandbox Code Playgroud) 使用stringr包,很容易以矢量化方式执行正则表达式替换.
问题:如何执行以下操作:
替换中的每个单词
hello,world??your,make|[]world,hello,pos
Run Code Online (Sandbox Code Playgroud)
不同的替代品,例如增加数量
1,2??3,4|[]5,6,7
Run Code Online (Sandbox Code Playgroud)
注意,不能假设简单的分隔符,实际用例更复杂.
stringr::str_replace_all 似乎没有用,因为它
str_replace_all(x, "(\\w+)", 1:7)
Run Code Online (Sandbox Code Playgroud)
为应用于所有单词的每个替换生成一个向量,或者它具有不确定和/或重复的输入条目,以便
str_replace_all(x, c("hello" = "1", "world" = "2", ...))
Run Code Online (Sandbox Code Playgroud)
不会为此目的而工作.
我有数据的地方如下
location<- c("xyz, sss, New Zealand", "USA", "Pris,France")
id<- c(1,2,3)
df<-data.frame(location,id)
Run Code Online (Sandbox Code Playgroud)
我想从数据中提取国家/地区名称.棘手的部分是,如果我只提取最后一个单词,那么我将只有一个记录(法国).
library(stringr)
df$country<- word(df$location,-1)
Run Code Online (Sandbox Code Playgroud)
关于如何从这些数据中提取国家数据的任何想法?
id location country
1 xyz, sss, New Zealand New Zealand
2 USA USA
3 Pris,France France
Run Code Online (Sandbox Code Playgroud) 大多数stringr函数只是相应stringi函数的包装器.str_replace_all就是其中之一.然而我的代码不适stri_replace_all用于相应的stringi函数.
我正在编写一个快速正则表达式来将(一个子集的)驼峰转换为间隔的单词.
我很困惑为什么这样做:
str <- "thisIsCamelCase aintIt"
stringr::str_replace_all(str,
pattern="(?<=[a-z])([A-Z])",
replacement=" \\1")
# "this Is Camel Case ain't It"
Run Code Online (Sandbox Code Playgroud)
而这不是:
stri_replace_all(str,
regex="(?<=[a-z])([A-Z])",
replacement=" \\1")
# "this 1s 1amel 1ase ain't 1t"
Run Code Online (Sandbox Code Playgroud) 我正在尝试在循环中使用stringi库中的函数stri_join ,但我遇到了困难。我想获取“A_1.png”,“A_2.png”,“A_3.png”,“A_4.png”,“A_5.png”,依此类推,直到“A_200.png”。
这是我的尝试:
x <- c(1:200)
x
for (i in 1:length(x)){
Names <-paste("A_", 1:length(i), ".png",sep = "")
print(Names)
}
Run Code Online (Sandbox Code Playgroud)
我获得“A_1.png”200次。如果你能指出我所缺少的东西。