- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
x<- c('a','v','c','a','d','e','g','f','h','y','u','r','s','w','s','d','g','j','u','r','s','s','s','v','b','g','e','w','s','d','g','h','j','i','t','e','w','w','q','q','d','v','b','m','m','k','l','u','p','o','r','t','n','e','w','w','j','f','c','g','h','t','r','d','e','w','w','w','z','f','g','f','h','h','y','r','f','f','l')
y <- sample(1:40, 79, replace=T)
y1 38 18 19 19 37 38 26 4 32 23 11 24 36 15 22 19 6 24 13 36 2 26 35 39 8 33 20 19 23 28 5 17 40 26 18 21[37] 35 23 27 12 3 33 16 32 11 19 4 5 8 19 5 19 33 33 33 13 12 32 21 4 14 8 28 34 33 22 34 19 39 23 6 8[73] 37 17 21 16 38 15 36
最佳答案
使用 dplyr
library(dplyr)
library(tidyr)
res <- tally(group_by(df, x, y=cut(y, breaks=seq(0,40, by=10)))) %>%
ungroup() %>%
spread(y,n, fill=0)
data.table
library(data.table)
res1 <- dcast.data.table(setDT(df)[,list(.N),
by=list(x, y1=cut(y, breaks=seq(0,40, by=10)))],
x~y1, value.var="N", fill=0L)
all.equal(as.data.frame(res), as.data.frame(res1))
#[1] TRUE
label
参数在
cut
所以如果你想拥有
column
标题要
freq0-10
, 等等
tally(group_by(df, x, y=cut(y,breaks=seq(0,40, by=10),
labels=paste0("freq", c("0-10", "10-20", "20-30", "30-40"))))) %>%
ungroup() %>%
spread(y,n, fill=0) %>%
head(2)
# x freq0-10 freq10-20 freq20-30 freq30-40
#1 a 0 1 1 0
#2 b 1 1 0 0
df <- structure(list(x = structure(c(1L, 22L, 3L, 1L, 4L, 5L, 7L, 6L,
8L, 24L, 21L, 18L, 19L, 23L, 19L, 4L, 7L, 10L, 21L, 18L, 19L,
19L, 19L, 22L, 2L, 7L, 5L, 23L, 19L, 4L, 7L, 8L, 10L, 9L, 20L,
5L, 23L, 23L, 17L, 17L, 4L, 22L, 2L, 13L, 13L, 11L, 12L, 21L,
16L, 15L, 18L, 20L, 14L, 5L, 23L, 23L, 10L, 6L, 3L, 7L, 8L, 20L,
18L, 4L, 5L, 23L, 23L, 23L, 25L, 6L, 7L, 6L, 8L, 8L, 24L, 18L,
6L, 6L, 12L), .Label = c("a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "y", "z"), class = "factor"), y = c(12L, 9L, 29L, 21L,
27L, 37L, 12L, 31L, 33L, 11L, 25L, 15L, 27L, 27L, 13L, 37L, 8L,
2L, 21L, 6L, 4L, 23L, 30L, 6L, 9L, 28L, 4L, 24L, 26L, 2L, 13L,
10L, 15L, 6L, 38L, 9L, 30L, 26L, 28L, 39L, 19L, 16L, 11L, 9L,
2L, 4L, 16L, 15L, 11L, 14L, 19L, 35L, 19L, 29L, 22L, 40L, 19L,
12L, 7L, 6L, 20L, 10L, 12L, 6L, 30L, 13L, 38L, 39L, 30L, 20L,
6L, 9L, 1L, 40L, 26L, 14L, 23L, 33L, 2L)), .Names = c("x", "y"
), row.names = c(NA, -79L), class = "data.frame")
关于r - 如何使用 dplyr 对 x 中的元素进行分组,在 y 的间隔内计算 x 的频率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26688494/
我有以下数据框: library(dplyr) df % rename_all(funs(stringr::str_replace_all(., "gh", "v"))) 我想结合使用 renam
我有以下数据框: library(dplyr) df % rename_all(funs(stringr::str_replace_all(., "gh", "v"))) 我想结合使用 renam
我有一个数据( df_1 ): df_1 % select_at(.vars = 'var_1') var_1 1 99.47262 10 25.91552 没关系。但: df_1
我正在尝试安装dplyr软件包,但收到一条错误消息,提示“库(dplyr)中存在错误:没有名为dplyr的软件包”。我正在使用窗口系统和Ri386 3.5.2。我尝试按照其他人的建议使用代码insta
假设我想以并行方式申请 myfunction到 myDataFrame 的每一行.假设 otherDataFrame是一个包含两列的数据框:COLUNM1_odf和 COLUMN2_odf出于某些原因
我目前正在构建一个包,我想知道是否有办法调用 %>%来自 dplyr 的操作符,而无需实际附加 dplyr 包。例如,对于从包中导出的任何函数,您可以使用双冒号 ( :: ) 调用它。所以如果我想使用
library(dplyr) mtcars %>% group_by(vs) %>% do(tt=t.test(mpg~am, data=.)) %>% mutate(t=tt$statist
我正在尝试为一组标准曲线构建一系列线性模型。 目前这段代码正在产生我想要的输出(每个线性模型的截距和斜率): slopes % group_by(plate, col, row, conc_ug_mL
我正在寻找替换我的一些使用 dplyr::do 的 R 代码,因为这个函数很快就会被弃用。我的很多工作都需要创建分层 CDF 图。使用 dply:do 时,我分层的变量作为变量传递给结果数据框,然后我
问题 我正在尝试使用 dplyr::mutate()和 dplyr::case_when()在数据框中创建新的数据列,该列使用存储在另一个对象(“查找列表”)中的数据填充,并基于数据框中列中的信息。
最近我发现了很棒的 dplyr.spark.hive启用 dplyr 的软件包前端操作 spark或 hive后端。 在包的 README 中有关于如何安装此包的信息: options(repos =
我正在尝试在 dplyr 链中使用 data.frame 两次。这是一个给出错误的简单示例 df % group_by(Type) %>% summarize(X=n()) %>% mu
当我浏览答案时 here , 我找到了 this solution与 data.frame 完全符合预期. library(dplyr) # dplyr_0.4.3 library(data.tab
我的数据来自一个数据库,根据我运行 SQL 查询的时间,该数据库可能包含一周到另一周不同的 POS 值。 不知道哪些值将在变量中使得自动创建报告变得非常困难。 我的数据如下所示: sample % p
我想定义与“扫帚”包中类似的功能 library(dplyr) library(broom) mtcars %>% group_by(am) %>% do(model = lm(mpg ~ w
set.seed(123) df % group_by(id) %>% mutate(roll.sum = c(x[1:4], zoo::rollapply(x, 5, sum))) # Groups
先来个样本数据 set.seed(123) dat 1 -4 2 6 3 -2 4
我有一个带列的数据框 x1, x2, group我想生成一个带有额外列的新数据框 rank表示x1的顺序在其组中。 有相关问题here ,但已接受的答案似乎不再有效。 到这里为止,很好: librar
我有一个示例 df,如下所示: d% group_by(CaseNo) %>% arrange(desc(Submissiondate)) %>% dplyr::mutate(rank = row_n
我有一个数据框,其中包含一些数据输入错误。 我希望将每组的这些异常值替换为每组最常见的值。 我的数据如下: df % group_by(CODE) %>% mutate(across(c(DOSAGE
我是一名优秀的程序员,十分优秀!