Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- firms <- paste0("Firm", sprintf("%03i", 1L:500L))
- brands <- paste0("Brand", sprintf("%03i", 1L:100L))
- brandsList <- replicate(length(firms), sample(brands, sample(1L:20L, 1L)))
- brandsFirms <- unlist(mapply(function(x, y) c(x, y), firms, brandsList, USE.NAMES = FALSE))
- head(brandsFirms, 24L)
- # [1] "Firm001" "Brand046" "Brand024" "Brand021" "Brand050" "Brand011"
- # [7] "Brand078" "Brand003" "Brand061" "Brand016" "Brand085" "Brand057"
- # [13] "Firm002" "Brand006" "Brand072" "Brand024" "Brand057" "Brand083"
- # [18] "Brand082" "Brand044" "Brand099" "Brand075" "Firm003" "Brand058"
- # 所以firms1是你的vector 1,brandsFirms是你的vector 2
- loc <- match(firms, brandsFirms)
- idx <- findInterval(seq_along(brandsFirms), loc)
- idx_firms <- firms[idx]
- idx_firms[loc] <- "firms"
- output <- split(brandsFirms, idx_firms)
- output$firms <- NULL
- head(output, 3L)
- # $Firm001
- # [1] "Brand046" "Brand024" "Brand021" "Brand050" "Brand011" "Brand078"
- # [7] "Brand003" "Brand061" "Brand016" "Brand085" "Brand057"
- #
- # $Firm002
- # [1] "Brand006" "Brand072" "Brand024" "Brand057" "Brand083" "Brand082"
- # [7] "Brand044" "Brand099" "Brand075"
- #
- # $Firm003
- # [1] "Brand058" "Brand069" "Brand047"
Advertisement
Add Comment
Please, Sign In to add comment