[英]How to put a list of vectors in a dataframe?
I'm not sure if the question captures it correctly but I have a dataframe that can be created with the following code我不确定问题是否正确捕获,但我有一个 dataframe 可以使用以下代码创建
library(dplyr)
counties <- c("aaa", "aaa", "aaa", "bbb", "bbb", "ccc", "ccc", "ccc", "ccc")
zips <- c(1321, 1321, 1322, 1523, 1567, 2102, 2102, 2134, 2140)
example_data <- data.frame(county = counties, zip = zips)
example_data_unique <- example_data %>%
select(county, zip) %>%
group_by(county, zip) %>%
slice(1) %>%
ungroup()
My end goal is to create a dataframe/tibble such that each county only appears once and the zip variable is now a vector of all zips in that county.我的最终目标是创建一个数据框/小标题,以便每个县只出现一次,并且 zip 变量现在是该县所有拉链的向量。 It would look something like this:
它看起来像这样:
county zip县 zip
aaa c(1321, 1322) aaa c(1321, 1322)
bbb c(1523, 1567) bbb c(1523, 1567)
We can use split
to create a list
of unique
values我们可以使用
split
创建unique
值list
lst1 <- with(unique(example_data), split(zip, county))
In dplyr
, it can be done with在
dplyr
中,可以使用
library(dplyr)
example_data %>%
distinct %>%
group_by(county) %>%
nest
Or wrap with list
in summarise
或者在
summarise
中用list
包装
example_data %>%
distinct %>%
group_by(county) %>%
summarise(zip = list(zip))
# A tibble: 3 x 2
# county zip
# <fct> <list>
#1 aaa <dbl [2]>
#2 bbb <dbl [2]>
#3 ccc <dbl [3]>
Here is a base R solution using aggregate
+ unique
这是使用
aggregate
+ unique
的基本 R 解决方案
dfout <- aggregate(.~county,example_data,unique)
such that这样
> dfout
county zip
1 aaa 1321, 1322
2 bbb 1523, 1567
3 ccc 2102, 2134, 2140
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.