不幸的是我吮吸regexp.如果我有这样的路径:
/long/path/to/file,我只需要接触file.
如果有人供应file/我只需要file.
如果有人供应/file/,我仍然需要file.
我一直在使用stringr函数作为拐杖,但这似乎是直截了当的grep领域.请帮忙?
我是新手,dplyr无法弄清楚如何控制变量来通过chaining(%>%)命令.简单的例子:该str_sub函数有三个参数 - 第一个是传递%>%但是如何获得最后两个?:
library(stringr)
library(dplyr)
df <- data.frame(V1 = c("ABBEDHH", "DEFGH", "EFGF", "EEFD"),
V2=c(4, 2, 1, 1), V3=c(5, 2, 2, 1), stringsAsFactors=FALSE)
Run Code Online (Sandbox Code Playgroud)
在基地RI可以做:
with(df, str_sub(V1, V2, V3))
Run Code Online (Sandbox Code Playgroud)
得到:
## [1] "ED" "E" "EF" "E"
Run Code Online (Sandbox Code Playgroud)
如何链接这个?- 我试过了:
df %>% str_sub(V1, V2, V3) # Here V3 is unused arg since V1 is treated as 2nd arg
df %>% select(V1) %>% str_sub(V2, V3) # Here V2 and V3 are not recognized
Run Code Online (Sandbox Code Playgroud) 我需要在字符串"Count of"之后找到数字."Count of"字符串和数字之间可能有空格或符号.我有一些适用于www.regex101.com的东西,但不适用于stringr str_extract功能.
library(stringr)
shopping_list <- c("apples x4", "bag of flour", "bag of sugar", "milk x2", "monkey coconut 3oz count of 5", "monkey coconut count of 50", "chicken Count Of-10")
str_extract(shopping_list, "count of ([\\d]+)")
[1] NA NA NA NA "count of 5" "count of 50" NA
Run Code Online (Sandbox Code Playgroud)
我想得到什么:
[1] NA NA NA NA "5" "50" "10"
Run Code Online (Sandbox Code Playgroud) 该stringr包有乐于助人str_replace()和str_replace_all()功能。例如
mystring <- "one fish two fish red fish blue fish"
str_replace(mystring, "fish", "dog") # replaces the first occurrence
str_replace_all(mystring, "fish", "dog") # replaces all occurrences
Run Code Online (Sandbox Code Playgroud)
太棒了 但是你怎么样
我有以下类型的数据帧
df <- tibble::tribble(~x,
c("A", "B"),
c("A", "B", "C"),
c("A", "B", "C", "D"),
c("A", "B"))
Run Code Online (Sandbox Code Playgroud)
和这些矢量
vec1 <- c("A", "B")
vec2 <- c("A", "B", "C")
vec3 <- c("A", "B", "C", "D")
Run Code Online (Sandbox Code Playgroud)
我想改变一个变量y,它显示哪一行有哪个向量.我尝试了以下方法,但是获取带有警告的空y变量:"较长的对象长度不是较短对象长度的倍数"
df_new <- df %>%
mutate(y = case_when(x == vec1 ~ "vec1",
x == vec2 ~ "vec2",
x == vec2 ~ "vec3"))
Run Code Online (Sandbox Code Playgroud)
期望的输出是
df_new <- tibble::tribble(~x, ~y,
c("A", "B"), "vec1",
c("A", "B", "C"), "vec2",
c("A", "B", "C", "D"), "vec3",
c("A", "B"), "vec1")
Run Code Online (Sandbox Code Playgroud) 我试图寻找解决方案,但似乎 R 没有明确的解决方案。
我尝试通过模式拆分字符串,比如说,空格和大写字母,我为此使用stringr包。
x <- "Foobar foobar, Foobar foobar"
str_split(x, " [:upper:]")
Run Code Online (Sandbox Code Playgroud)
通常我会得到:
[[1]]
[1] "Foobar foobar," "oobar foobar"
Run Code Online (Sandbox Code Playgroud)
但是,我想得到的输出应该包括来自分隔符的字母:
[[1]]
[1] "Foobar foobar," "Foobar foobar"
Run Code Online (Sandbox Code Playgroud)
在 stringr 中可能没有像反向引用这样的开箱即用的解决方案,所以我很乐意得到任何帮助。
我有一个列表,其中包含每个观察的多个字符串(见下文)。
[1] A, C, D
[2] P, O, E
[3] W, E, W
[4] S, B, W
Run Code Online (Sandbox Code Playgroud)
我想测试字符串是否包含某些子字符串,如果是,则返回相应的子字符串,在此示例中,这将是“A”或“B”(请参阅下面的所需结果)。每个观察将只包含 2 个子串 (A|B) 中的一个
[1] A
[2] NA
[3] NA
[4] B
Run Code Online (Sandbox Code Playgroud)
不,我已经尝试解决这个问题,但它似乎效率很低,而且我也没有让它工作。我怎么能解决呢?
if (i == "A") {
type <- "A"
} else if { (i == "B")
type <- "B"
} else { type <- "NA"
}
Run Code Online (Sandbox Code Playgroud)
注意:我需要遍历 > 1000 次观察
# Sample Data Frame
df <- data.frame(Column_A
=c("1011 Red Cat",
"Mouse 2011 is in the House 3001", "Yellow on Blue Dog walked around Park"))
Run Code Online (Sandbox Code Playgroud)
我有一列试图清除的手动输入数据。
Column_A
1|1011 Red Cat |
2|Mouse 2011 is in the House 3001 |
2|Yellow on Blue Dog walked around Park|
Run Code Online (Sandbox Code Playgroud)
我想将每个特征分成其自己的列,但仍保留列A以在以后提取其他特征。
Colour Code Column_A
1|Red |1001 |Cat
2|NA |2001 3001 |Mouse is in the House
3|Yellow on Blue |NA |Dog walked around Park
Run Code Online (Sandbox Code Playgroud)
到目前为止,我一直在用gsub重新排列它们并捕获组,然后使用Tidyr :: extract分离它们。
library(dplyr)
library(tidyr)
library(stringr)
df1 <- df %>%
# Reorders …Run Code Online (Sandbox Code Playgroud) 我有一个问题,我试图从包含文本和数字的字符串中提取数字,然后创建两个新列,显示数字的最小值和最大值。
例如,我有一列和一串这样的数据:
Text
Section 12345.01 to section 12345.02
Run Code Online (Sandbox Code Playgroud)
我想从 Text 列中的数据创建两个新列,如下所示:
Min Max
12345.01 12345.02
Run Code Online (Sandbox Code Playgroud)
我将 dplyr 和 stringr 与正则表达式一起使用,但正则表达式仅提取模式的第一次出现(第一个数字)。
df%>%dplyr::mutate(SectionNum = stringr::str_extract(Text, "\\d+.\\d+"))
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用该stringr::str_extract_all功能。它似乎提取了模式的两个出现,但它在小标题中创建了一个列表,我发现这是一个真正的麻烦。所以我坚持第一步,只是想把数字放到他们自己的列中。
谁能推荐最有效的方法来做到这一点?理想情况下,我想从字符串中提取数字,将它们转换为数字as.numeric,然后运行min()和max()运行。
我有一些像下面这样的字符串。我需要从字符串中提取颜色部分。
s1= 'color: red greenSize: 2 CountVerified Purchase'
s2= 'color: red greenVerified Purchase'
s3= 'color: red greenSize: 2 Count'
s4= 'color: red green'
Run Code Online (Sandbox Code Playgroud)
我str_replace像下面那样使用。它仅适用于s1和s3。不适合s2和s4。
str_replace(s1, 'color:\\s(.*)Size:\\s.*', '\\1')
Run Code Online (Sandbox Code Playgroud)
有谁知道我该如何从适用于所有4种情况的字符串中提取颜色?