Ramakrishna S
Ramakrishna S

Reputation: 437

data in multiple columns in one single column or row

I would like my data in one single column or row. Any suggestions

library(purrr)
1:2 %>% map(~ sample(1:10, 5, replace = T))
#> [[1]]
#> [1] 8 1 5 8 4
#> 
#> [[2]]
#> [1]  5  4  8 10 10

Desired output: 8 1 5 8 4 5 4 8 10 10 or

8 
1 
5 
8 
4 
5  
4  
8 
10 
10

Upvotes: 0

Views: 37

Answers (3)

ThomasIsCoding
ThomasIsCoding

Reputation: 101373

You can try sapply + c like below

c(sapply(1:2, function(k) sample(10, 5, replace = TRUE)))

Upvotes: 0

akrun
akrun

Reputation: 887118

We can use rerun

library(purrr)
2 %>%
  rerun(sample(1:10, 5, replace = TRUE))

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388982

You can unlist the output from map.

unlist(1:2 %>% purrr::map(~ sample(1:10, 5, replace = T)))

However, I think this is more of a replicate job.

c(replicate(2, sample(1:10, 5, replace = T)))

Upvotes: 3

Related Questions