Your example is vague, so I'll make some inferences:
- The vectors are manually- and individually- handled; if there is a way to deal with them programmatically (such as a list of disparate vectors, or a naming convention that allows simple automation), it will make some of the below easier.
- "... each vector in its own column" means that the columns are next to each other, not consecutive. I have dealt with CSV files that have multiple matrices with different numbers of columns; this is not normal, and not quite right.
Some data:
set.seed(42)
vec1 <- sample(100, size = 5)
vec2 <- sample(100, size = 8)
vec3 <- sample(100, size = 9)
In general, when you try to add columns to a matrix or data.frame in R, it really wants the number of rows (or length of the vector) to be convenient; when not, it complains. So, we can force it some.
df <- data.frame(vec1 = rep(NA, max(sapply(list(vec1, vec2, vec3), length))))
df[1:length(vec1), 1] <- vec1
df[1:length(vec2), 2] <- vec2
df[1:length(vec3), 3] <- vec3
df
## V1 V2 V3
## 1 92 52 26
## 2 93 73 46
## 3 29 14 93
## 4 81 64 95
## 5 62 68 12
## 6 NA 44 99
## 7 NA 96 53
## 8 NA 87 85
## 9 NA NA 13
Now we can write it to a CSV file:
write.csv(df, file = 'somefile.csv', row.names = FALSE, na = '')
Note that I included na = ''
as an option; otherwise, your CSV file will have NA
strings in the extra space between the end of each vector and the end of the longest vector. Blanks are typically interpreted better (though that's up to you and/or with whom you share your data).