我有以下数据集。正如你所看到的,我有两周的一些定量数据,我想对他们的日子进行比较(即:星期一 09 和星期一 10 ):
week date day n
(chr) (date) (chr) (int)
1 Week 09 2016-02-29 Monday 5535
2 Week 09 2016-03-01 Tuesday 7497
3 Week 09 2016-03-02 Wednesday 8658
4 Week 09 2016-03-03 Thursday 6113
5 Week 09 2016-03-04 Friday 4553
6 Week 09 2016-03-05 Saturday 2
7 Week 10 2016-03-07 Monday 5339
8 Week 10 2016-03-08 Tuesday 6196
9 Week 10 2016-03-09 Wednesday 5395
10 Week 10 2016-03-10 Thursday 5633
Run Code Online (Sandbox Code Playgroud)
我得到了下面的代码,但是日期是无序的。无论如何我可以按时间顺序排序这些天:
ggplot(data = my_data, aes(x = as.factor(x …Run Code Online (Sandbox Code Playgroud) 下面是一些简单示例数据框和绘图的代码。我想知道如何有条件地为条形着色。我熟悉scale_fill_manual手动为条形着色,但是如果我想"Satisfied"在2016构面中使用不同的颜色,如果它的百分比"Satisfied"低于2015. 也许是红色警告边框或不同的颜色,例如橙色(仅作为示例)。
这不是最好的例子,但如果我有一个逐年最高框分数的图,那么当条形下降到一定百分比以下时,这对于让条形改变颜色会很有用。我尝试使用"colour = ifelse(Perc < 60, "orange", "green"组合,但无法使其发挥作用。我不确定如何构造该ifelse语句或将其放置在 ggplot 代码中的位置。
Year<-c(2015, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2016)
Service<-c("Satisfied", "Satisfied", "Satisfied", "Dissatisfied", "Dissatisfied",
"Satisfied", "Satisfied", "Dissatisfied", "Dissatisfied", "Dissatisfied")
df <- data.frame(Year, Service)
library(dplyr)
df.prop <- df %>%
count(Year, Service) %>%
mutate(Perc = prop.table(n))
library(ggplot2)
ggplot(df.prop, aes(x = Service, y = Perc, fill = Service)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label …Run Code Online (Sandbox Code Playgroud) 有没有办法summarise在dplyr同一列上(或与其他代码),而不必每次都指定列?
summarise(temp1, earliest=min(ArrDelay), average= mean(ArrDelay), latest=(ArrDelay))
Run Code Online (Sandbox Code Playgroud) 我需要ggplot2来绘制特定X处所有值的总和.它的作用只需要一个值.
我的数据摘录df:
Day S V
1 2016-12-27 K 60
2 2016-12-27 K 600
3 2016-12-27 M 80
4 2016-12-27 M 695
Run Code Online (Sandbox Code Playgroud)
完整数据str(df):
'data.frame': 52 obs. of 3 variables:
$ Day : POSIXlt, format: "2016-12-15" "2016-12-15" "2016-12-15" ...
$ S : chr "K" "K" "M" "M" ...
$ V : num 50 560 255 460 110 500 145 460 40 630 ...
Run Code Online (Sandbox Code Playgroud)
我使用下面的代码来制作图表,但由于某种原因,只显示值600和695.我希望它显示660和775.
ggplot(data = daneDzien, aes(x = Day, y = V, fill = S)) +
geom_bar(stat …Run Code Online (Sandbox Code Playgroud) 一位同事有一些数据由许多稀疏列组成,这些列应该折叠成几个填充列.例如:
d1 <- data.frame(X1 = c(rep("Northampton", times=3), rep(NA, times=7)),
X2 = c(rep(NA, times=3), rep("Amherst", times=5), rep(NA, times=2)),
X3 = c(rep(NA, times=8), rep("Hadley", times=2)),
X4 = c(rep("Stop and Shop", times=2), rep(NA, times=6), rep("Stop and Shop", times=2)),
X5 = c(rep(NA, times=2), rep("Whole Foods", times=6), rep(NA, times=2)))
d1
X1 X2 X3 X4 X5
1 Northampton <NA> <NA> Stop and Shop <NA>
2 Northampton <NA> <NA> Stop and Shop <NA>
3 Northampton <NA> <NA> <NA> Whole Foods
4 <NA> Amherst <NA> <NA> Whole Foods …Run Code Online (Sandbox Code Playgroud) 似乎在R中,我可以引用带有变量名称一部分的变量.但我很困惑为什么我能做到这一点.
使用以下代码作为示例:
library(car)
scatterplot(housing ~ total)
house.lm <- lm(housing ~ total)
summary(house.lm)
str(summary(house.lm))
summary(house.lm)$coefficients[2,2]
summary(house.lm)$coe[2,2]
Run Code Online (Sandbox Code Playgroud)
当我打印摘要(house.lm)的结构时,我得到以下输出:
> str(summary(house.lm))
List of 11
$ call : language lm(formula = housing ~ total)
$ terms :Classes 'terms', 'formula' language housing ~ total
.. ..- attr(*, "variables")= language list(housing, total)
.. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:2] "housing" "total"
.. .. .. ..$ : chr "total"
.. ..- …Run Code Online (Sandbox Code Playgroud) 我已经阅读了几个关于dplyr编程的指南,我仍然对如何解决使用非标准评估(NSE)评估构造/连接字符串的问题感到困惑.我意识到有更好的方法来解决这个例子,而不是使用NSE,但想要学习如何.
t <- tibble( x_01 = c(1, 2, 3), x_02 = c(4, 5, 6))
i <- 1
Run Code Online (Sandbox Code Playgroud)
这是我想要的结果,但是想要mutate()构造变量:
t %>% mutate(d_01 = x_01 * 2)
#> A tibble: 3 x 3
#> x_01 x_02 d_01
#> <dbl> <dbl> <dbl>
#> 1 1.00 4.00 2.00
#> 2 2.00 5.00 4.00
#> 3 3.00 6.00 6.00
Run Code Online (Sandbox Code Playgroud)
这是我第一次尝试使用字符串:
new <- sprintf("d_%02d", i)
var <- sprintf("x_%02d", i)
t %>% mutate(new = var * 2)
#> Error in mutate_impl(.data, dots) : …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的字符串(变量和常量的数量并不重要):
> my_string <- "-x+2y+z=-1; x-3y-2z=-1; 3x-y-z=4"
Run Code Online (Sandbox Code Playgroud)
我知道如何cbind(A, b)使用替换和数字函数获取矩阵...
# [,1] [,2] [,3] [,4]
# [1,] -1 2 1 -1
# [2,] 1 -3 -2 -1
# [3,] 3 -1 -1 4
Run Code Online (Sandbox Code Playgroud)
......但不知道如何自动获得两个矩阵A和b
A
# [,1] [,2] [,3]
# [1,]-1 2 1
# [2,] 1 -3 -2
# [3,] 3 -1 -1
b
# [,1]
# [1,]-1
# [2,]-1
# [3,] 4
Run Code Online (Sandbox Code Playgroud)
这意味着我如何分割这个字符串=以获得一个矩阵,其中数字元素位于等号前面,另一个矩阵元素位于它之后?
编辑.到目前为止我做了这个:
my_string<-"-x+2y+z=-1; x-3y-2z=-1; 3x-y-z=4"
my_string<-gsub('([[:punct:]]|\\s)([a-z])', '\\11\\2', my_string)
my_string<-stringr::str_replace_all(my_string,"[a-z]"," ")
my_string<-stringr::str_replace_all(my_string,"; …Run Code Online (Sandbox Code Playgroud) 我有下表,其中我有一个汽车备件的项目名称.我有汽车制造商生产的特定零件的ITEM代码,我也有零件制造商生产的相同零件的相应ITEM代码.
我定期获得一个输入,我只获得已售出的ITEM代码.如何识别出售的部件.
> trial
# A tibble: 6 x 5
Name `OEM Part` `OES 1 Code` `OES 2 Code` `OES 3 Code`
<chr> <chr> <chr> <chr> <chr>
1 Brakes 231049A76 1910290/230023 NA NA
2 Cables 2410ASD12 NA 219930 3213Q23
3 Tyres 9412HJ12 231233 NA NA
4 Suspension 756634K71 782320/880716 NA NA
5 Ball Bearing 2IW2WD23 231224 NA NA
6 Clutches 9304JFW3 NA QQW223 23RQR3
Run Code Online (Sandbox Code Playgroud)
假设我输入了以下值
> item_code <- c("231049A76", "1910290", "1910290", "23RQR3")
Run Code Online (Sandbox Code Playgroud)
我需要以下输出
Name
Brakes
Brakes
Brakes
Clutches
Run Code Online (Sandbox Code Playgroud)
注:该 …
我在运行线性回归后遇到问题predict,因为我无法弄清楚哪些 X 变量实际上包含在线性回归中。
假设我运行模型:
model1 <- lm(outcome ~ employee + shape + size + color + I(color^2)
data = data)
Run Code Online (Sandbox Code Playgroud)
回归输出中确定的观测值数量为 224605。
当我尝试像这样运行预测时:
test = data.frame(y = predict(model1), x = data$employee)
Error in data.frame(y = predict(model1), x = data$employee) :
arguments imply differing number of rows: 224605, 233262
Run Code Online (Sandbox Code Playgroud)
我认为我可以获得正确数量的观察结果,如下所示:
> test = na.omit(data, cols = all.vars(model1))
> nrow(test)
[1] 207256
Run Code Online (Sandbox Code Playgroud)
但这仍然不能产生正确的观察数量。有没有直接的方法来获取线性回归实际使用的观察结果?