How to Cbind or Rbind Different Lengths Vectors Without Repeating the Elements of the Shorter Vectors

How to concatenate vectors of different lengths without recycling and without using a loop?

Find the length of the longest vector and set the length of each vector to that, then concatenate.

v1 <- c("A", "B", "C", "D", "E")
v2 <- c("F", "G", "H")

n <- max(length(v1), length(v2))

length(v1) <- n
length(v2) <- n

paste(v1, v2, sep = "-")
#> [1] "A-F" "B-G" "C-H" "D-NA" "E-NA"

Created on 2021-09-26 by the reprex package (v2.0.1)

Sum of columns from different data frames (different lengths) in R

Here it is shown how we could do it:

#data:
df1 <- tibble(x1 = c(1,4,4,6))
df2 <- tibble(x2 = c(1,4,4,6,6,6,8))
df3 <- tibble(x3 = c(1,4,4,6,6))

# 1. construct a list
df_list <- list(df1, df2, df3)

#install.packages("qpcR")
library(qpcR)

# 2. Use `cbind.na` from gpcR package to fill the lacking length with `NA`, so all columns have the same length:
df_result <- do.call(qpcR:::cbind.na, df_list)

# 3. Use `rowSums` to sum
df_result$final <- rowSums(df_result, na.rm = TRUE)

# 4. save as dataframe
final_x <- data.frame(final = df_result[,4])

# 5. call result:
final_x
  final
1 3
2 12
3 12
4 18
5 12
6 6
7 8

(Row)binding vectors with different lengths by pattern

You can create a list column in a data frame and then unnest it.

library(dplyr)
library(tidyr)

l <- mget(ls(pattern="million_cities_*"))

tibble(cities = l) %>%
unnest_wider("cities)

The main annoyance is the message about "new names" that you will get for each row.

# A tibble: 3 x 9
...1 ...2 ...3 ...4 ...5 ...6 ...7 ...8 ...9
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 Toronto Montreal Calgary Ottawa Edmonton NA NA NA NA
2 London Birmingham NA NA NA NA NA NA NA
3 New York Los Angeles Chicago Houston Phoenix Philadelphia San Antonio San Diego Dallas

You can avoid that by jumping to purrr so that you can create named tibble rows.

library(purrr)
library(tibble)

map_dfr(l, ~ as_tibble_row(set_names(.x, seq.int(.x))))
# A tibble: 3 x 9
`1` `2` `3` `4` `5` `6` `7` `8` `9`
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 Toronto Montreal Calgary Ottawa Edmonton NA NA NA NA
2 London Birmingham NA NA NA NA NA NA NA
3 New York Los Angeles Chicago Houston Phoenix Philadelphia San Antonio San Diego Dallas

How to combine two dataframes columns containing differing number of elements in R?

Not sure if the code below is what you are after

subset(
merge(
cbind(id = 1:nrow(dfa), dfa),
cbind(id = 1:nrow(dfb), dfb),
all = TRUE
),
select = -id
)

which gives

    a  b
1 1 1
2 2 2
3 3 3
4 NA 4
5 NA 5
6 NA 6
7 NA 7
8 NA 8
9 NA 9
10 NA 10

Dummy Data

dfa <- data.frame(a = 1:3)
dfb <- data.frame(b = 1:10)

If you have multiple data frames more than 2, you can try the following base R code

subset(
Reduce(
function(...) merge(..., all = TRUE),
lapply(list(dfa, dfb, dfc), function(x) cbind(id = 1:nrow(x), x))
),
select = -id
)

which gives

    A  B  C
1 1 1 1
2 2 2 2
3 3 3 3
4 NA 4 4
5 NA 5 5
6 NA 6 6
7 NA 7 7
8 NA 8 NA
9 NA 9 NA
10 NA 10 NA

Dummy Data

dfa <- data.frame(A = 1:3)
dfb <- data.frame(B = 1:10)
dfc <- data.frame(C = 1:7)

How to convert a list consisting of vector of different lengths to a usable data frame in R?

try this:

word.list <- list(letters[1:4], letters[1:5], letters[1:2], letters[1:6])
n.obs <- sapply(word.list, length)
seq.max <- seq_len(max(n.obs))
mat <- t(sapply(word.list, "[", i = seq.max))

the trick is, that,

c(1:2)[1:4]

returns the vector + two NAs



Related Topics



Leave a reply



Submit