该perl()函数在最新版本的 stringr 中已被弃用,取而代之的是regex(). 但是,我似乎无法复制早期的行为。
要将字符串向量的第一个字母大写,可以使用以下方法:
name <- c("jim", "john", "bill")
str_replace(name, perl("^(.)"), "\\U\\1")
Run Code Online (Sandbox Code Playgroud)
然而,这不再有效:
str_replace(name, regex("^(.)"), "\\U\\1")
Run Code Online (Sandbox Code Playgroud)
但使用基本 R 是有效的:
gsub("^(.)", "\\U\\1", name, perl=TRUE)
Run Code Online (Sandbox Code Playgroud)
还有办法用 stringr 包来做到这一点吗?
对于 R 来说还是比较新的。我有一列推文,我正在尝试创建一个包含转发句柄“RT @blahblah”的列,如下所示:
Tweets Retweetfrom
RT @john I had a good day RT @john
RT @josh I had a bad day RT @josh
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
r$Retweetfrom <- str_extract_all(r$Tweets, "^RT[:space:]+@[:graph:]+")
Run Code Online (Sandbox Code Playgroud)
它给了我正确的结果,但新列不是向量,而是列表。当我尝试取消列出它时,它会抛出一个错误:
Error in `$<-.data.frame`(`*tmp*`, "Retweetfrom", value = c("@AlpineITW", "@AllScienceGlobe", : replacement has 1168 rows, data has 2306
Run Code Online (Sandbox Code Playgroud)
有人知道如何处理这个问题吗?多谢。
我想将一列字符串(例如[1,58,10])与tidyr分开使用.我的问题是有时候列更短(永远不会更长).我在同一数据框中有很多列有此问题.
加载包
require(tidyr)
require(dplyr)
require(stringr)
Run Code Online (Sandbox Code Playgroud)
数据
在这里,我使用来自真实数据的样本制作数据框."载体"在col1中长度为10,在col2中为9或10.有一个时间列只是为了显示还有其他列.
df <- data.frame(
time = as.POSIXct(1:5, origin=Sys.time()),
col1 = c("[0,355,0,0,0,1227,0,0,382059,116]", "[0,31,0,0,0,5,0,0,925,1]", "[0,1,0,0,0,471,0,0,130339,3946]", "[0,0,0,0,0,223,0,0,37666,12]", "[0,19,0,0,0,667,0,0,336956,53]"),
col2 = c("[0,355,0,0,0,1227,0,0,382059,116]", "[0,355,0,0,0,1227,0,0,382059,116]", "[0,0,0,0,0,223,0,0,37666,12]", "[0,19,0,0,0,667,0,0,336956]","[0,355,0,0,0,1227,0,0,382059,116]")
)
Run Code Online (Sandbox Code Playgroud)
我多么想要它
对于所有"向量"长度相等的第一列,我可以使用separate()来获得我想要的.
a1 <- df %>%
mutate(col1 = str_sub(col1,2,-2)) %>%
separate(col1, paste("col1",1:10,sep="."),",")
# Making sure the numbers are numeric
a1 <- as.data.frame(sapply(a1, as.numeric)) %>%
mutate(time = as.POSIXct(time, origin="1970-01-01")) %>% select(-col2)
Run Code Online (Sandbox Code Playgroud)
这导致了
> a1
time col1.1 col1.2 col1.3 col1.4 col1.5 col1.6 col1.7 col1.8
1 2014-11-07 12:21:45 0 355 0 0 0 1227 0 …Run Code Online (Sandbox Code Playgroud) 样本数据
files.in.path = c("a.4.0. name 2015 - NY.RDS",
"b.4.0. name 2016 - CA.RDS",
"c.4.0. name 2015 - PA.RDS")
strings.to.find = c("4.0", "PA")
Run Code Online (Sandbox Code Playgroud)
我想要显示包含所有元素的逻辑向量strings.to.find。结果想要:
FALSE FALSE TRUE
Run Code Online (Sandbox Code Playgroud)
此代码将查找包含以下任何一项的元素strings.to.find,即,使用 OR 运算符
str_detect(files.in.path, str_c(strings.to.find, collapse="|")) # OR operator
TRUE TRUE TRUE
Run Code Online (Sandbox Code Playgroud)
此代码尝试使用 AND 运算符但不起作用。
str_detect(files.in.path, str_c(strings.to.find, collapse="&")) # AND operator
FALSE FALSE FALSE
Run Code Online (Sandbox Code Playgroud)
这在几行中有效,我可以编写一个for循环,该循环将为具有大量strings.to.find
det.1 = str_detect(files.in.path, "4.0" )
det.2 = str_detect(files.in.path, "PA" )
det.all = det.1 & det.2
FALSE FALSE TRUE
Run Code Online (Sandbox Code Playgroud)
但是有没有更好的方法不涉及使用依赖于strings.to.find.
是否有一个stringr等效grep与value设置为TRUE?(我想避免下面NA的stringr命令返回的。)
library(stringr)
x <- c("a", "b", "a")
grep("a", x, value = TRUE) # returns "a" "a"
str_extract(x, "a") # returns "a" NA "a"
Run Code Online (Sandbox Code Playgroud) 如何使用str_match提取最后一个子字符串之后的剩余字符串。
例如,对于字符串“带奶油的苹果,橙子和香蕉”,我想在最后一次出现“和”之后提取该字符串的其余部分,以返回“香蕉和奶油”。
我尝试了此命令的许多替代方法,但它要么一直返回第一个“和”之后的字符串其余部分,要么返回空字符串。
library(stringr)
str_match("apples and oranges and bananas with cream", "(?<= and ).*(?! and )")
# [,1]
#[1,] "oranges and bananas with cream"
Run Code Online (Sandbox Code Playgroud)
我已经在StackOverflow上搜索了解决方案,并找到了一些针对javascript,Python和base R的解决方案,但没有找到针对stringer包的解决方案。
谢谢。
我想提取除模式之外的所有内容并将其返回到字符串中。
我试图将 str_extract_all 与 sapply 和 cat 结合在一起
x = c("a_1","a_20","a_40","a_30","a_28")
data <- tibble(age = x)
# extracting just the first pattern is easy
data %>%
mutate(age_new = str_extract(age,"[^a_]"))
# combining str_extract_all and sapply doesnt work
data %>%
mutate(age_new = sapply(str_extract_all(x,"[^a_]"),function(x) cat(x,sep="")))
class(str_extract_all(x,"[^a_]"))
sapply(str_extract_all(x,"[^a_]"),function(x) cat(x,sep=""))
Run Code Online (Sandbox Code Playgroud)
返回 NULL 而不是串联模式
我想在stringr包中使用str_extract从表单的字符串中提取数字XX nights etcetc。
我目前正在这样做:
library(stringr)
str_extract("17 nights$5 Days", "(\\d)+ nights")
Run Code Online (Sandbox Code Playgroud)
但这又回来了
"17 nights"
代替17。
如何只提取数字?我以为用括号指定提取组会起作用,但是没有用。
我有这样的文字:
text = 'I love apple, pear, grape and peach'
Run Code Online (Sandbox Code Playgroud)
如果我想知道文本是否包含apple或pear。我可以执行以下操作并且工作正常:
str_detect(text,"apple|pear")
[1] TRUE
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果我想像这样使用布尔值怎么办(apple OR pear) AND (grape)。无论如何我可以把它放进去str_detect()。那可能吗?以下是不工作:
str_detect(text,"(apple|pear) & (grape)" )
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
我想知道这一点的原因是我想编程以将“布尔查询”转换为grepor str_detect。就像是:
str_detect(text, '(word1|word2) AND (word2|word3|word4) AND (word5|word6) AND .....')
Run Code Online (Sandbox Code Playgroud)
数量AND不一....
请没有多个解决方案str_detect。
我有这个字符串:
[1] "19980213" "19980214" "19980215" "19980216" "19980217" "iffi" "geometry"
[8] "date_consid"
Run Code Online (Sandbox Code Playgroud)
我想匹配所有不是日期而不是“date_consid”的元素。我试过
res = grep("(?!\\d{8})|(?!date_consid)", vec, value=T)
Run Code Online (Sandbox Code Playgroud)
但我就是不能让它工作......