【发布时间】:2020-09-06 20:12:18
【问题描述】:
我有一个相当简单的问题: 如果您有一个原始数据集,然后您通过过滤数据集来计算值,以便为您提供一个问题的答案:您如何构建数据框/答案的小标题?
#load the packages
library(easypackages)
packages("tidyverse","readxl","sf","tmaptools","tmap","lubridate",
"lwgeom","Cairo","nngeo","purrr","scales", "ggthemes","janitor")
polls<-st_as_sf(read.csv(url("https://www.caerphilly.gov.uk/CaerphillyDocs/FOI/Datasets_polling_stations_csv.aspx")),
coords = c("Easting","Northing"),crs = 27700)%>%
mutate(date = sample(seq(as.Date('2020/01/01'), as.Date('2020/05/31'), by="day"), 147))
test_stack<-polls%>%st_join(polls%>%st_buffer(dist=1000),join=st_within)%>%
filter(Ballot.Box.Polling.Station.x!=Ballot.Box.Polling.Station.y)%>%
add_count(Ballot.Box.Polling.Station.x)%>%
rename(number_of_neighbours = n)%>%
mutate(interval_date = date.x-date.y)%>%
subset(select = -c(6:8,10,11,13:18))## removing this comment will summarise the data so that only number of neighbours is returned %>%
distinct(Ballot.Box.Polling.Station.x,number_of_neighbours,date.x)%>%
filter(number_of_neighbours >=2)
polls%>%mutate(id = as.numeric(row_number()))%>% mutate(thing = case_when(id %% 2 == 0 ~ "stuff",
id %% 2 !=0 ~ "type"))->polls
polls%>%filter(thing=="stuff"& Polling.District.Code =="AC")%>%count()
polls%>%filter(thing == "type" & Polling.District.Code =="IA")%>%count()
如何构建行名有意义且列是计算值的数据框?
有点像
行名值
东西 AC 1
类型 IA 1
【问题讨论】:
-
tidyr包中的pivot_longer采用包含许多列的“宽”数据集,并创建一个“长”数据集,其中列名成为行标识符。然后,您可以组合这些行标识符来创建您想要的名称。