我知道這個問題很尷尬。如果我可以更好地表達它,我可能會在其他執行緒中找到解決方案。
我有這個資料結構......
df <- data.frame(group = c("X", "F", "F", "F", "F", "C", "C"),
subgroup = c(NA, "camel", "horse", "dog", "cat", "orange", "banana"))
...并想把它變成這個...
data.frame(group = c("X", "F", "camel", "horse", "dog", "cat", "C", "orange", "banana"))
......這令人驚訝地令人困惑。另外,我寧愿不使用回圈。
編輯:我更新了示例以澄清不幸的是依賴于排序的解決方案不能解決問題。
uj5u.com熱心網友回復:
這是帶有新資料的(已編輯)答案。使用data.table會有很大幫助。我們的想法是將 df 分成幾組,并將lapply()我們需要的東西分成每組。同時還要處理一些事情。
library(data.table)
# set as data.table
setDT(df)
# to mantain the ordering, you need to put as factor the group.
# the levels are going to give the ordering infos to split
df[,':='(group = factor(group, levels =unique(df$group)))]
# here the split function, splitting df int a list
df_list <-split(df, df$group, sorted =F)
# now you lapply to each element what you need
df_list <-lapply(df_list, function(x) data.frame(group = unique(c(as.character(x$group),x$subgroup))))
# put into a data.table and remove NAs
rbindlist(df_list)[!is.na(df_onecol$group)]
group
1: X
2: F
3: camel
4: horse
5: dog
6: cat
7: C
8: orange
9: banana
uj5u.com熱心網友回復:
一種解決方案,至少適用于當前示例(我們可以使用安排來查找組的正確順序)。
df %>% mutate(col1 = 1) %>% # add column for the pivot_longer
# pivot groups into one column
pivot_longer(-col1, values_to = 'group') %>%
# arrange the groups alphabetically.
arrange(group) %>%
# remove duplicate rows.
unique() %>%
# remove the NA.
filter(!is.na(group))
## alternatively if you also have other data than just these
## two columns the code below should work.
df %>%
mutate(col1 = 1) %>%
pivot_longer(c(group, subgroup), values_to = 'group') %>%
arrange(group) %>%
# group by the new group column and keep the first row
group_by(group) %>%
slice(1) %>%
ungroup() %>%
filter(!is.na(group))
#output
# A tibble: 9 × 3
col1 name group
<dbl> <chr> <chr>
1 1 group A
2 1 group B
3 1 subgroup BA
4 1 subgroup BB
5 1 subgroup BC
6 1 subgroup BD
7 1 group C
8 1 subgroup CA
9 1 subgroup CB
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/529082.html
標籤:r数据框
