Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I am having a list of data X with two vectors as follows:

X[1]=(1,2,3,5,6,9,7,8)

X[2]=(2,3,4,5,6)

Can I get a new list of data Y as follows?

Y[1]=(1,2,3,5,6,9,7,8,1,2,3,5,6,9,7,8)-repeat x[1]

Y[2]=(2,3,4,5,6,2,3,4,5,6)-repeat x[2]

For that, I have used the Y<-rep(X,2) but get

Y[1]:(1,2,3,5,6,9,7,8)

Y[2]:(2,3,4,5,6)

Y[3]:(1,2,3,5,6,9,7,8)

Y[4]:(2,3,4,5,6)

How to do it right? 

1 Answer

0 votes
by (107k points)

Simply you can use the sapply/lapply, in R programming:

sapply(X, rep, 2)

#[[1]]

# [1] 1 2 3 5 6 9 7 8 1 2 3 5 6 9 7 8

#[[2]]

# [1] 2 3 4 5 6 2 3 4 5 6

#The data is:

X <- list(c(1, 2, 3, 5, 6, 9, 7, 8), c(2, 3, 4, 5, 6))

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...