小编ska*_*han的帖子

随机抽样的行,"每个条件至少有一个"

所以我有一个看起来像这样的数据集:

a   b   c
23  34  Falcons
14  9   Hawks
2   18  Eagles
3   21  Eagles
22  8   Falcons
11  4   Hawks
Run Code Online (Sandbox Code Playgroud)

而且我知道我可以使用嵌套条件执行随机的行子集,但我想要做的是创建一个随机子集,该子集至少占用列'c'中每个可用值中的一个.

所以可能是正确的子集

23  34  Falcons
14  9   Hawks
3   21  Eagles
Run Code Online (Sandbox Code Playgroud)

要么

11  4   Hawks
2   18  Eagles
22  8   Falcons
Run Code Online (Sandbox Code Playgroud)

[没有特别的顺序],但是像:

14  9   Hawks
2   18  Eagles
3   21  Eagles
Run Code Online (Sandbox Code Playgroud)

不行,因为'猎鹰'没有代表.在R中有一个简单的方法吗?

r dplyr

6
推荐指数
1
解决办法
778
查看次数

for leaflet for leafletproxy中的多个多边形?

我正在波士顿对邻居多边形进行中途停留的可视化.这是一个按比例缩小的工作代码:

library(shiny)
library(leaflet)
library(plyr)
library(dplyr)
library(rgdal)

#setwd
setwd("C:/Users/580048/Downloads")

#read hubway station data
hubway <- read.csv("Hubway_Stations.csv")

#read shapefiles
neighborhoods <-readOGR("C:/Users/580048/Downloads/bosneigh/Bos_neighborhoods_new.shp","Bos_neighborhoods_new")
neighborhoods <- spTransform(neighborhoods, CRS("+proj=longlat +datum=WGS84"))

#ui layout
ui <- bootstrapPage(

  #style of tags
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  tags$style(type = "text/css", 'label[for="range"] {color: white;}'),
  tags$style(type = "text/css", 'label[for="range2"] {color: white;}'),
  tags$style(type = "text/css", 'label[for="team"] {color: white;}'),
  tags$style(type = "text/css", 'label[for="away"] {color: white;}'),
  tags$style(type = "text/css", 'label {color: white;}'),


  #the map
  leafletOutput("bosmap", width = "100%", height = "100%")

)


#server functions …
Run Code Online (Sandbox Code Playgroud)

r polygon leaflet shiny

3
推荐指数
1
解决办法
574
查看次数

R中有group_by的条件累积求和?

所以我有一个ID事件,我想使用group_by(或一些类似的函数)来做一个条件累积和.这是数据:

ID  Event
42  NA
42  1
42  2
42  NA
42  1
43  NA
43  1
43  2
43  2
Run Code Online (Sandbox Code Playgroud)

而我想要做的是有两个新的列,累计计算1和2,而不会折叠任何数据:

ID  Event   count_1s    count_2s
42  NA      0           0
42  1       1           0
42  2       1           1
42  NA      1           1
42  1       2           1
43  NA      0           0
43  1       1           0
43  2       1           1
43  2       1           2
Run Code Online (Sandbox Code Playgroud)

所以我理解如何使用group_by将它们总结为ID,如下所示:

t <- data %>% group_by(ID, Event) %>% summarize(count_1s = sum(!is.na(Event == 1)))
Run Code Online (Sandbox Code Playgroud)

但我无法理解的是如何获得一个运行的条件总和 - 似乎group_by将崩溃我的数据,我需要维护每一行.

编辑:所以接受的答案是完美的,但只是一个问题.如果价值因事件不同怎么办?例如:

ID …
Run Code Online (Sandbox Code Playgroud)

conditional r cumulative-sum

1
推荐指数
1
解决办法
171
查看次数

标签 统计

r ×3

conditional ×1

cumulative-sum ×1

dplyr ×1

leaflet ×1

polygon ×1

shiny ×1