假设我有这个data.frame(有3个变量)
ID Period Score
123 2013 146
123 2014 133
23 2013 150
456 2013 205
456 2014 219
456 2015 140
78 2012 192
78 2013 199
78 2014 133
78 2015 170
Run Code Online (Sandbox Code Playgroud)
使用dplyr我可以按ID对它们进行分组,并过滤出现多次出现的ID
data <- data %>% group_by(ID) %>% filter(n() > 1)
Run Code Online (Sandbox Code Playgroud)
现在,我想要实现的是添加一个列:差异=期间P的得分 - 期间P-1的得分得到这样的结果:
ID Period Score Difference
123 2013 146
123 2014 133 -13
456 2013 205
456 2014 219 14
456 2015 140 -79
78 2012 192
78 2013 199 7
78 2014 133 -66 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用count()帮助器过滤行.我想要的输出是所有的行map %>% count(StudentID) = 3.
例如,在下面的df中,它应该取出所有具有StudentID 10016和10020的行,因为它们只是这些的2个实例,我想要3.
StudentID StudentGender Grade TermName ScaleName TestRITScore
100 M 9 Fall 2010 Language Usage 217
100 M 10 2011-2012 Language Usage 220
100 M 9 Fall 2010 Reading 210
10016 M 6 Fall 2010 Language Usage 217
10016 M 6 Fall 2010 Mathematics 210
10020 F 7 Fall 2010 Language Usage 210
10020 F 7 Fall 2010 Mathematics 213
10022 F 8 Fall 2010 Language Usage 232
10022 F 9 …Run Code Online (Sandbox Code Playgroud) 我最近开始使用 dygraph,到目前为止我非常喜欢它。我曾尝试使用它闪亮,但没有取得太大成功。虽然我的脚本没有产生任何错误,但它也没有产生任何图形!
你有没有机会指导我朝着正确的方向前进?
这是我的数据示例:
> head(df2)
date Variety Count Price Value Quantity TotalKg
1 2014-11-06 CRIPPS PINK 80-90 204 3670 18 333
2 2014-11-06 CRIPPS PINK 120-135 181 10150 56 1036
3 2014-11-06 CRIPPS RED 80-90 221 26910 122 2257
4 2014-11-06 CRIPPS RED 100-110 205 22910 112 2072
5 2014-11-06 CRIPPS RED 120-135 193 58950 306 5661
6 2014-11-06 TOPRED 80-90 167 7350 44 814
Run Code Online (Sandbox Code Playgroud)
使用 Variety 和 Count 变量,我想绘制价格随时间变化的图表。
这是我的 ui.R
library(dygraphs)
library(shiny)
shinyUI(fluidPage(
titlePanel("Apples Prices"),
sidebarLayout(
sidebarPanel( …Run Code Online (Sandbox Code Playgroud) 我想在数据框中用另一个字符串替换某个字符串,这是一个示例代码:
table_ex <- data.frame(row.names = c("row 1", "row 2", "row 3"))
table_ex$year1 <- 3:1
table_ex$year2 <- c("NaN", 5, "NaN %")
table_ex$year3 <- c("NaN %", 7, "NaN %")
remove_symb <- function(yolo){stringr::str_replace(yolo, 'NaN %|NaN', '')}
table_ex <- mutate_all(table_ex, funs(remove_symb))
Run Code Online (Sandbox Code Playgroud)
执行上述操作是删除我的rownnames。我知道我可以使用 lapply 函数,但我想知道为什么行名被删除了。是因为str_replace功能还是mutate_all功能?我应该如何防止这种情况?
我制作了一个适合我的R脚本,但我知道我可以通过使用函数使它更好(更漂亮).不幸的是,我的各种尝试并不成功.谁能引导我走上正轨?以下是我的原始脚本.
library(dplyr)
apples <- read.csv("JoburgApples.csv")
grs <- apples %>% filter(grepl("GRANNY", ProductName), tvaluesold >10000) %>% mutate(Variety = "Granny Smith")
cpp <- apples %>% filter(grepl("PINK", ProductName), tvaluesold >10000) %>% mutate(Variety = "Cripps Pink")
top <- apples %>% filter(grepl("TOP", ProductName), tvaluesold >10000) %>% mutate(Variety = "Top Red")
gld <- apples %>% filter(grepl("GOLDEN", ProductName), tvaluesold >10000) %>% mutate(Variety = "Golden Delicious")
ski <- apples %>% filter(grepl("STARKING", ProductName), tvaluesold >10000) %>% mutate(Variety = "Starking")
bra <- apples %>% filter(grepl("BRAEBURN", ProductName), tvaluesold >10000) %>% mutate(Variety = …Run Code Online (Sandbox Code Playgroud)