I have col3 and col4, both have 5000 rows. I want to make a new column that has 10,000 rows, the first 5000 being col3 and the second 5000 being col4. Or if it's easier they do not actually need to be in order.
Asked
Active
Viewed 1,411 times
-1
-
Please add data using `dput` or something that we can copy and use. Also show expected output for the data shared. Read about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Nov 28 '20 at 04:01
-
> test$col5 <- dplyr::bind_rows(test$col3, test$col4) Error: Argument 1 must have names. – Derpman5000 Nov 28 '20 at 04:09
2 Answers
0
are these vestors? A reproducible example would be nice, but some variation of this should work
new_col <- rbind(col3,col4)

sjp
- 820
- 1
- 5
- 10
0
Let's first create a sample dataset (as you haven't given)
set.seed(50)
df <- data.frame(col3 = runif(500),
col4 = runif(500))
> head(df)
col3 col4
1 0.70872710 0.1396443
2 0.43765986 0.6179266
3 0.20000490 0.8179890
4 0.76706599 0.2320035
5 0.51316189 0.9072317
6 0.04470388 0.2108911
Nor proceed to append like this (Note you have to create this is as new data frame as previous one has lesser rows than required)
df_new <- data.frame(new_col = c(df$col3, df$col4))
> (tibble(df_new))
# A tibble: 1,000 x 1
new_col
<dbl>
1 0.709
2 0.438
3 0.200
4 0.767
5 0.513
6 0.0447
7 0.700
8 0.646
9 0.0420
10 0.108
# ... with 990 more rows
In case, you have other columns in data which you have to preserve along with this append, then proceed as follows
set.seed(50)
df <- data.frame(id = 1:500,
col3 = runif(500),
col4 = runif(500))
(tibble(df))
# A tibble: 500 x 3
id col3 col4
<int> <dbl> <dbl>
1 1 0.540 0.0111
2 2 0.522 0.509
3 3 0.371 0.803
4 4 0.526 0.274
5 5 0.342 0.922
6 6 0.450 0.512
7 7 0.383 0.698
8 8 0.651 0.0556
9 9 0.491 0.748
10 10 0.677 0.728
# ... with 490 more rows
#Process of append
library(dplyr)
df %>% pivot_longer(cols = c(col3, col4)) %>% select(-name)
# A tibble: 1,000 x 2
id value
<int> <dbl>
1 1 0.540
2 1 0.0111
3 2 0.522
4 2 0.509
5 3 0.371
6 3 0.803
7 4 0.526
8 4 0.274
9 5 0.342
10 5 0.922
# ... with 990 more rows

AnilGoyal
- 25,297
- 4
- 27
- 45
-
> test6$col5 <- rbind(test6$col3, test6$col4) Error in `$<-.data.frame`(`*tmp*`, col5, value = c(2.5, NA, -1.5, NA, : replacement has 2 rows, data has 5144 – Derpman5000 Nov 28 '20 at 04:48