R - Create a repetitive list from a smaller length vector to fit into dataframe

0 votes
How to create a repetitive list (field) from a smaller length vector to fit into dataframe? I want to add the vector as a column by repetiting the vector.
Aug 21, 2019 in Data Analytics by nithin
622 views

1 answer to this question.

0 votes

Hi Nithin,

rep() is used to replicate a vector, so you can use it for your problem.

Syntax: rep(vector,replication_number).

Ex: rep(c(1:5),5)

To add the replicated vector to a data frame, you can give replication_number as [number of rows]/[size of vector]

For suppose if the data frame consists of 23 rows and vector has 5 elements,  then use code as 

length.out is used to mention the size of the new vector after replication. It ignores is the size of replicated is more than data frame size.

> z = data.frame(x=1:23,y=21:43)
> z
    x  y
1   1 21
2   2 22
3   3 23
4   4 24
5   5 25
6   6 26
7   7 27
8   8 28
9   9 29
10 10 30
11 11 31
12 12 32
13 13 33
14 14 34
15 15 35
16 16 36
17 17 37
18 18 38
19 19 39
20 20 40
21 21 41
22 22 42
23 23 43
> z$vec = rep(c(1:5),5,length.out = nrow(z))
> z
    x  y vec
1   1 21   1
2   2 22   2
3   3 23   3
4   4 24   4
5   5 25   5
6   6 26   1
7   7 27   2
8   8 28   3
9   9 29   4
10 10 30   5
11 11 31   1
12 12 32   2
13 13 33   3
14 14 34   4
15 15 35   5
16 16 36   1
17 17 37   2
18 18 38   3
19 19 39   4
20 20 40   5
21 21 41   1
22 22 42   2
23 23 43   3
answered Aug 21, 2019 by anonymous
• 33,030 points

Related Questions In Data Analytics

+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,661 views
0 votes
1 answer
+1 vote
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 14, 2018 in Data Analytics by Sahiti
• 6,370 points
7,038 views
0 votes
1 answer

How to import data from a Json file into R?

Easy and simple: library("rjson") json_file <- "file_name" json_data <- fromJSON(file=json_file)) or ...READ MORE

answered Apr 26, 2018 in Data Analytics by DeepCoder786
• 1,720 points
2,359 views
0 votes
1 answer

By using dpylr package sum of multiple columns

Basically here we are making an equation ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
2,250 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,920 views
0 votes
1 answer

In a dpylr pipline how to use sample and seq?

For avoiding rowwise(), I prefer to use ...READ MORE

answered Apr 6, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by Gitika 1,182 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,341 views
0 votes
1 answer

How to create a tree like structure from R list/vector?

Hi, puja. Use below function to create a ...READ MORE

answered Aug 30, 2019 in Data Analytics by anonymous
• 33,030 points
967 views
+1 vote
5 answers

How to remove NA values from a Vector in R?

Hello team, you can use na.omit x <- c(NA, 3, ...READ MORE

answered Dec 9, 2020 in Data Analytics by anonymous
• 82,840 points
195,125 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP