我有一长串的对象需要分成更小的列表,每个列表有20个条目。问题是每个对象只能在一个列表中出现一次。
# Create some example data...
# Make a list of objects.
LIST <- c('Oranges', 'Toast', 'Truck', 'Dog', 'Hippo', 'Bottle', 'Hope', 'Mint', 'Red', 'Trees', 'Watch', 'Cup', 'Pencil', 'Lunch', 'Paper', 'Peanuts', 'Cloud', 'Forever', 'Ocean', 'Train', 'Fork', 'Moon', 'Horse', 'Parrot', 'Leaves', 'Book', 'Cheese', 'Tin', 'Bag', 'Socks', 'Lemons', 'Blue', 'Plane', 'Hammock', 'Roof', 'Wind', 'Green', 'Chocolate', 'Car', 'Distance')
# Generate a longer list, with a random sequence and number of repetitions for each entry
LONG.LIST <- data.frame(Name = (sample(LIST, size = 200, replace = TRUE)))
print(LONG.LIST)
Name
1 Cup
2 Distance
3 Roof
4 Pencil
5 Lunch
6 Toast
7 Watch
8 Bottle
9 Car
10 Roof
11 Lunch
12 Forever
13 Cheese
14 Oranges
15 Ocean
16 Chocolate
17 Socks
18 Leaves
19 Oranges
20 Distance
21 Green
22 Paper
23 Red
24 Paper
25 Trees
26 Chocolate
27 Bottle
28 Dog
29 Wind
30 Parrot
etc....
使用上面生成的示例,'Distance'
出现在两个位置&#39; 2&#39;并且&#39; 20&#39;,'Lunch'
同时在&#39; 5&#39;和&#39; 11,'Oranges'
at&#39; 14&#39;和19&#39;,所以没有重复的第一个列表需要扩展到包括'Green'
,'Paper'
和'Red'
。然后,第二个列表将从位置24的'Paper'
开始。
最后一个列表可能不完整,所以最好用“NA”来填充
如果输出是单个数据框中的列,那将是最简单的。
我不知道从哪里开始,所以任何建议都非常感谢。谢谢!
答案 0 :(得分:3)
我们可以使用tidyverse
执行此操作。按名称&#39;分组,创建一个包含序列号的列,我们在group_by
中使用该列创建新的序列列&#39; ind&#39;,然后转换为&#39; wide& #39;格式为spread
和order
按字母顺序排列
library(tidyverse)
LONG.LIST %>%
group_by(Name) %>%
mutate(grp = row_number()) %>%
group_by(grp) %>%
mutate(ind = row_number()) %>%
spread(grp, Name) %>%
mutate_at(vars(-one_of("ind")), funs(.[order(as.character(.))]))
# A tibble: 40 x 12
# ind `1` `2` `3` `4` `5` `6` `7` `8` `9` `10` `11`
# <int> <fctr> <fctr> <fctr> <fctr> <fctr> <fctr> <fctr> <fctr> <fctr> <fctr> <fctr>
# 1 1 Bag Bag Bag Bag Bag Bag Bag Bag Cup Distance Distance
# 2 2 Blue Blue Book Book Book Cloud Cup Cup Distance Train NA
# 3 3 Book Book Bottle Cloud Cloud Cup Distance Distance Train NA NA
# 4 4 Bottle Bottle Cheese Cup Cup Distance Dog Hammock NA NA NA
# 5 5 Car Car Cloud Distance Distance Dog Hammock Moon NA NA NA
# 6 6 Cheese Cheese Cup Dog Dog Hammock Moon Parrot NA NA NA
# 7 7 Chocolate Chocolate Distance Fork Hammock Horse Paper Train NA NA NA
# 8 8 Cloud Cloud Dog Hammock Horse Moon Parrot NA NA NA NA
# 9 9 Cup Cup Fork Hippo Mint Paper Train NA NA NA NA
#10 10 Distance Distance Green Horse Moon Parrot NA NA NA NA NA
# ... with 30 more rows