假设我有这样的数据集:
id <- c(1, 1, 2, 2, 3, 3)
code <- c("a", "b", "a", "a", "b", "b")
dat <- data.frame(id, code)
Run Code Online (Sandbox Code Playgroud)
也就是说,
id code
1 1 a
2 1 b
3 2 a
4 2 a
5 3 b
6 3 b
Run Code Online (Sandbox Code Playgroud)
使用dplyr,我如何计算每个id的数量
即
id countA
1 1 1
2 2 2
3 3 0
Run Code Online (Sandbox Code Playgroud)
我正在尝试这样不起作用的东西,
countA<- dat %>%
group_by(id) %>%
summarise(cip.completed= count(code == "a"))
Run Code Online (Sandbox Code Playgroud)
上面给出了一个错误,"错误:没有适用于'group_by_'的方法应用于类的对象"逻辑""
谢谢你的帮助!
假设我有这样的数据集:
dat <- data.frame
text = c(
"It made me feel very positive to brand X",
"It was clear and easy to understand",
"I didn't like it al all"),
value=runif(3)
)
Run Code Online (Sandbox Code Playgroud)
我可以使用包装中的TradeGothic LT CondEighteen字体在ggplot中绘制它extrafonts:
library(ggplot2)
p <- ggplot(dat, aes(text, value)) +
geom_bar(stat="identity") +
coord_flip() +
labs(title=" Do you agree with the following statements?")+
theme_bw(16)+
theme(text=element_text(family="TradeGothic LT CondEighteen"))
ggsave('plot.pdf', plot = plot, path = "/Users/jacobdeecurtis/Desktop")
Run Code Online (Sandbox Code Playgroud)
但是当我ggplot_gtable在剧情中使用时:
gt <- ggplot_gtable(ggplot_build(plot))
gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- …Run Code Online (Sandbox Code Playgroud) 我有一个数据集,我有兴趣查看测试分数和经历事件的人的百分比:
dat <- data.frame(score = 1:7,
n.event = c(263,5177,3599,21399,16228,10345,1452),
n.total = c(877,15725,13453,51226,32147,26393,7875),
percentage = c(30,33,27,42,50,39,18))
Run Code Online (Sandbox Code Playgroud)
我可以用图表上的百分比来绘制它,如下所示:
ggplot(data=dat, aes(x=score, y=percentage)) +
geom_line() +
geom_text(aes(label = paste0(dat$percentage,"%")))
Run Code Online (Sandbox Code Playgroud)
或者我可以用这样的分数绘制它:
ggplot(data=dat, aes(x=score, y=percentage)) +
geom_line() +
geom_text(aes(label = paste0("frac(",dat$n.event, ",", dat$n.total,
")")),parse = TRUE)
Run Code Online (Sandbox Code Playgroud)
但我想把它们并排放置。这不起作用:
ggplot(data=dat, aes(x=score, y=percentage)) +
geom_line() +
geom_text(aes(label = paste0(dat$percentage,"%","frac(",dat$n.event,
",", dat$n.total, ")")),parse = TRUE)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
解析错误(text = as.character(lab)) : :1:3: 意外输入 1: 30%frac(263,877) ^
感谢您的帮助!
我有一个这样的数据集:
id date value
1 8/06/12 1
1 8/08/12 1
2 8/07/12 2
2 8/08/12 1
Run Code Online (Sandbox Code Playgroud)
每个 id 都应该有每个日期的值。当 id 缺少特定日期时,需要为该行添加值 0。例如,
id date value
1 8/06/12 1
1 8/07/12 0
1 8/08/12 1
2 8/06/12 0
2 8/07/12 2
2 8/08/12 1
Run Code Online (Sandbox Code Playgroud)
我正在尝试找出如何添加带有 0 的行。这里有一个很好的解决方案:R - 按组填充缺失的日期。但是,我无法使用该函数,因为据我所知,我tidyr::complete正在使用该函数,并且需要保留在函数内。sparklyrdplyr
我需要生成一千个 R Markdown 文件,每个文件本质上都是相同的,只是具有不同的标题和不同的数据子集。有没有办法将这些标题和子集存储在对象中并自动创建和编织 R Markdown 文件?
我正在尝试使用ORDER BY在SQL中创建一个volatile表,我收到一个错误.
CREATE VOLATILE TABLE orderd_dates AS
(SELECT * FROM date_table
ORDER BY id_date)
with data primary index (id_date) on commit preserve rows;
Run Code Online (Sandbox Code Playgroud)
错误是:子查询中不允许ORDER BY.
如果我不能使用order by,如何创建一个有序的易失性表?
假设我有这样的数据集:
id <- rep(1, 9)
start_over <- c(rep(NA, 3), "yes", NA, "yes", rep(NA, 3))
dat <- data.frame(id, start_over)
Run Code Online (Sandbox Code Playgroud)
也就是说,
id start_over
1 1 NA
2 1 NA
3 1 NA
4 1 yes
5 1 NA
6 1 yes
7 1 NA
8 1 NA
9 1 NA
Run Code Online (Sandbox Code Playgroud)
如何创建一个新变量,每次start_over为"是"时,每个变量递增1.
即
id start_over assignment
1 1 NA 1
2 1 NA 1
3 1 NA 1
4 1 yes 2
5 1 NA 2
6 1 yes 3
7 1 NA …Run Code Online (Sandbox Code Playgroud) 我有一个字符串,它是一个日期(月,日,年).
char <- "12/1/2014"
Run Code Online (Sandbox Code Playgroud)
当我尝试将此字符串转换为日期时,我指定月,日,年
date <- as.Date(char, origin = "%m/%d/%Y")
Run Code Online (Sandbox Code Playgroud)
它像我放入年,日,月那样格式化:
> date
[1] "0012-01-20"
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
r ×7
dplyr ×2
ggplot2 ×2
apache-spark ×1
as.date ×1
count ×1
create-table ×1
fonts ×1
geom-text ×1
gtable ×1
parsing ×1
r-markdown ×1
sparklyr ×1
sql ×1
sql-order-by ×1
subquery ×1
teradata ×1