可以使用
slice
和
rep
复制原始数据帧的每一行,使其具有与新数据帧相同的行数(权重类数*字符数):
#Duplicate original dataframe, each row repeat number of weightclasses times
longer_starwars<- starwars %>% slice(rep(1:n(), each = length(boxingWeightClassName)))
#Combine the new columns with the old (only the first 6 columns, for readability)
starwarsBoxingEligibility<- cbind(longer_starwars[,1:6], allowed)
head(starwarsBoxingEligibility
以上给出了我想要的结果(见下文),尽管也许有更好的方法:
name height mass hair_color skin_color eye_color class criterion allowed
1 Luke Skywalker 172 77 blond fair blue Featherweight 57 FALSE
2 Luke Skywalker 172 77 blond fair blue Middleweight 80 TRUE
3 Luke Skywalker 172 77 blond fair blue Heavyweight 92 TRUE
4 Luke Skywalker 172 77 blond fair blue Super Heavyweight 9999 TRUE
5 C-3PO 167 75 <NA> gold yellow Featherweight 57 FALSE
6 C-3PO 167 75 <NA> gold yellow Middleweight 80 TRUE