我有一个问题,我试图从包含文本和数字的字符串中提取数字,然后创建两个新列,显示数字的最小值和最大值。
例如,我有一列和一串这样的数据:
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()运行。
我遇到过这样一种情况:我有这样的数据:
df <- data.frame(id = 1:1000,
x = sample(0:30, 1000, replace = T),
y = sample(50:10000, 1000, replace = T))
Run Code Online (Sandbox Code Playgroud)
我想分配另一个z基于多个条件的列,即
if x <= 5 & y <= 100, z = 1
if x > 5 & x <= 10 & y <= 100, z = 2
if x > 10 & x <= 12 & y <= 100, z = 3
if x > 12 & x <= 20 & y <= 100, z = 4
if …Run Code Online (Sandbox Code Playgroud) 也许是一个简单的问题,如何生成矢量的组合.我有下一个矢量.
> x<-1:5
> x
[1] 1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
所以,我要的是所有这些的组合,但该序列不能包含在组合相同数量和ab==ba,abc==bca==cab即:
permut<-c(1:5,12,13,14,15,23,24,25,34,...,123,124,125,134,135,...,1234,1235,...)
permut
[1] 1 2 3 4 5 12 13 14 15 23 24 25 34 ... 123 124 125 134 135 ... 1234 1235
Run Code Online (Sandbox Code Playgroud)
我认为这个功能expand.grid()很有用,但我不知道它是如何使用的.
这是我到目前为止已经完成的数据是数字数据类型
if (is.na(data) || attribute==0){replace(data,NA)}
Run Code Online (Sandbox Code Playgroud)
它给了我错误信息
替换(属性,NA)错误:缺少参数“值”,没有默认值
我正在制作一个快速抓取项目,涉及抓住历史NFL足球数据.以下是我的数据的快速浏览:
allgames_thisweek = c("Chicago Bears 21, Tampa Bay Buccaneers 9 -- Box Score", "Cleveland Browns 28, Cincinnati Bengals 20 -- Box Score",
"Dallas Cowboys 26, Pittsburgh Steelers 9 -- Box Score", "Detroit Lions 31, Atlanta Falcons 28 (OT) -- Box Score",
"Green Bay Packers 16, Minnesota Vikings 10 -- Box Score", "Indianapolis Colts 45, Houston Oilers 21 -- Box Score",
"Kansas City Chiefs 30, New Orleans Saints 17 -- Box Score",
"Los Angeles Rams 14, Arizona Cardinals 12 -- Box …Run Code Online (Sandbox Code Playgroud) 假设我有一个示例数据框:
frame <-
data.frame(group = c(rep(1, 3), rep(2, 3)),
idea = c(1, 2, 3, 1, 2, 4),
value = c(10000, 5000, 50, 5000, 7500, 100),
level = sample(c("rough", "detailed"), 6, TRUE))
Run Code Online (Sandbox Code Playgroud)
我想要一个值的条形图,其中组内的每个想法按其值排序。我可以这样靠近
library(dplyr)
library(ggplot2)
top_ideas <-
frame %>%
group_by(group) %>%
arrange(group, desc(value))
frame %>%
group_by(group) %>%
mutate(idea = idea %>% factor(levels = top_ideas$idea)) %>%
ggplot(aes_string(x = "idea", y = "value", fill = "level")) +
geom_bar(stat = "identity") +
theme(legend.position = "bottom",
axis.text.x = element_text(angle = 45, vjust = 1, hjust …Run Code Online (Sandbox Code Playgroud) 我在R中有一个数据框。我的目标是创建一个带if_else语句的新列。如果一行中的值等于string "company",则此新列的值将是data列中的值。否则,我想分配给if NA值。
我不知道如何实现,下面的代码不起作用。由于数据类型不同。
library(dplyr)
active_labels <- data %>%
mutate(start_date = if_else(type == "company", date, NA)
Error in mutate_impl(.data, dots) :
Evaluation error: `false` must be type double, not logica
Run Code Online (Sandbox Code Playgroud) 假设我有一个非常大的相关表,并且只想检查大于某个值的相关性(例如,0.40).如何提取值大于0.40的所有行或列?
我可以使用apply执行此操作,但希望在tidyverse中执行操作.
library(tidyverse)
df <- mtcars %>%
select_if(is.numeric) %>%
cor() %>%
round(digits = 2) %>%
as.data.frame()
df[apply(df, 1, function(row) {all(abs(row) > .40)}),]
Run Code Online (Sandbox Code Playgroud) r ×8
dataframe ×2
if-statement ×2
combinations ×1
dplyr ×1
ggplot2 ×1
regex ×1
string ×1
stringr ×1
tidyverse ×1