如何在R语言中合并split生成的列表dfa与dfb?
Merging Two Lists in R: dfa + dfb = dfc
Hey there! The issue here is that cbind() and rbind() are built for combining data frames or matrices by columns/rows—they don’t handle list element concatenation the way you need.
Luckily, merging your two lists into one (with dfa elements first, followed by dfb elements) is super straightforward with base R’s c() function. Here’s the solution:
# Combine dfa and dfb into dfc dfc <- c(dfa, dfb)
Why this works:
When you use c() on two lists, it appends all elements of the second list directly to the end of the first list. This perfectly fits your requirement:
dfc[[1]]todfc[[4]]will exactly matchdfa[[1]]todfa[[4]]dfc[[5]]todfc[[15]]will align withdfb[[1]]todfb[[11]]- The total length of
dfcwill be4 + 11 = 15, just like you want.
Verify the result
You can quickly confirm everything is in order with these checks:
# Confirm total length is 15 length(dfc) # Check that the first element matches dfa's first element identical(dfc[[1]], dfa[[1]]) # Check that the 5th element matches dfb's first element identical(dfc[[5]], dfb[[1]])
All of these should return TRUE if the merge worked correctly.
内容的提问来源于stack exchange,提问作者Aaron Walton
相关产品推荐
相关产品推荐

