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 20 in Data Analytics by nithin
13 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 20 by anonymous
• 25,900 points

Related Questions In Data Analytics

0 votes
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,700 points
79 views
0 votes
1 answer
0 votes
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 13, 2018 in Data Analytics by darklord
• 6,140 points
142 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,700 points
165 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,700 points
97 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,700 points
118 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,700 points
76 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,700 points
70 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 in Data Analytics by anonymous
• 25,900 points
12 views
0 votes
4 answers

How to remove NA values from a Vector in R?

You can try na.omit() or na.exclude() too. ...READ MORE

answered Jul 31 in Data Analytics by anonymous
• 25,900 points
6,239 views