Novice, we appreciate that you are new to R. But please study a few basics. In particular how vector recycle.
Your problem:
vec1 <- c(1,2,3)
vec2 <- c("A","B","C","D","E")
df <- data.frame(var1 = vec1, var2 = vec2)
Error in data.frame(var1 = vec1, var2 = vec2) :
arguments imply differing number of rows: 3, 5
You may "glue" vectors together with cbind
- check out the warning. The problem of different vector length is not gone.
df <- cbind(vec1, vec2)
Warning message:
In cbind(vec1, vec2) :
number of rows of result is not a multiple of vector length (arg 1)
What you get - vec1 is "recycled". In principle R assumes you want to fill the missing places by repeating the values ... (which might not what you want).
df
vec1 vec2
[1,] "1" "A"
[2,] "2" "B"
[3,] "3" "C"
[4,] "1" "D"
[5,] "2" "E"
## you can convert this to a data frame, if you prefer that object structure
Warning message:
In cbind(vec1, vec2) :
number of rows of result is not a multiple of vector length (arg 1)
> df
vec1 vec2
1 1 A
2 2 B
3 3 C
4 1 D
5 2 E
So your approach to extend the unequal vector length with NA is valid (and possibly what you want).
Thus, you are on the right way.
- determine the length of your longest vector
- inject NAs where needed (mind you you may not want to have them always at the end)
This problem can be found on Stackoverflow. Check out
How to cbind or rbind different lengths vectors without repeating the elements of the shorter vectors?