How to populate new variable with repeated values from vector?
how to repeat a sequence in r
r repeat vector to matrix
r rep function
create a sequence of even numbers between 1 to 10 repeating twice. in r
r duplicate elements in vector
array of repeated values r
repeat a vector
I have a vector of values:
values = c(22, 42, 243)
I have a variable in a dataframe:
df$variable = 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3
How do I repeat each value in the values vector n times in a new variable in my dataframe such that I get the following:
df$new_variable = 22, 22, 22, 22, 42, 42, 42, 42, 243, 243, 243, 243
The simplest way is to use sapply
:
sapply(variable, function(x) df$values[x])
Hm... There is even a simpler solution:
values[df$variable]
Create sequence of repeated values, in sequence?, You missed the each= argument to rep() : R> n <- 3 R> rep(1:5, each=n) [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 R>. so your example can be done with� To repeat the vector c(0, 0, 7) three times, use this code: > rep(c(0, 0, 7), times = 3) [1] 0 0 7 0 0 7 0 0 7. You also can repeat every value by specifying the argument each, like this: > rep(c(2, 4, 2), each = 3) [1] 2 2 2 4 4 4 2 2 2. R has a little trick up its sleeve. You can tell R for each value how often it has to be repeated.
You could use rle
and inverse.rle
r <- rle(variable)
r
is an object of class "rle"
r #Run Length Encoding # lengths: int [1:3] 4 4 4 # values : num [1:3] 1 2 3
Replace its values slot with your values
and call inverse.rle
to get desired output
r$values <- values inverse.rle(r) # [1] 22 22 22 22 42 42 42 42 243 243 243 243
data
values = c(22, 42, 243) variable = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)
How to Repeat Vectors in R, In R, you can combine a vector with itself if you want to repeat it, but if you want to repeat the values in a vector many times, using the c() function becomes a bit� The second column would be used for data that are sorted by week. Create ID vectors in SAS/IML software. One way to create ID vectors in SAS/IML software is to use the REPEAT function. The REPEAT function creates a matrix from an input vector by repeating the vector a specified number of times horizontally and vertically.
If variable
is not meant to be the index elements of values
, then we could do
rep(values, tapply(variable, variable, length)) # [1] 22 22 22 22 42 42 42 42 243 243 243 243
Or, with rle()
rep(values, rle(variable)$lengths) # [1] 22 22 22 22 42 42 42 42 243 243 243 243
Replicate Elements of Vectors and Lists, an integer-valued vector giving the (non-negative) number of times to repeat each element if of length length(x) , or to repeat the whole vector if of length 1. Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. creating a vector that repeats a number 360 times. Learn more about vector, repeating
Repeat copies of array elements - MATLAB repelem, If n is a scalar, then each element of v is repeated n times. The length Create a vector and repeat each of its elements three times into a new vector. v = [1 2 3� Now lets discuss different ways to add new columns to this data frame in pandas. Add column to dataframe in pandas using [] operator Pandas: Add new column to Dataframe with Values in list. Suppose we want to add a new column ‘Marks’ with default values from a list. Let’s see how to do this,
Repeat copies of array - MATLAB repmat, This MATLAB function returns an array containing n copies of A in the row and column Create a 3-by-2 matrix whose elements contain the value 10. repmat repeats the entries of the table and appends a number to the new variable names . 4) Initialize a vector by specifying size and elements Algorithm Begin Initialize a variable s. Create a vector v with size s and all values with 7. Initialize vector v1 by array. Initialize vector v2 by v1. Print the elements. End. Example. Live Demo
rep: Replicate Elements of Vectors and Lists, A vector giving the number of times to repeat each element if of length length(x) , or to repeat the whole vector if of length 1. length.out: non-negative integer. The� I don't do time-series analysis, but your data has an id variable which means that the time variable, week, is repeated by id. Correct? The command var however appears to expect only a single time-series.
Comments
- What if
df$variable
would bedf$variable + 5
? Doesn't seem to be a problem for OP though. - Glad you added
values[variable]
- Awesome, great solution! I appreciate the simplicity. Thank you.
- @markus in that case do
values[as.integer(factor(df$variable))]
- Of course, that case comes with the assumption that OP intends to match
variable
tovalue
positionally based just on the order of occurrence... seems like a very odd case. Ifdf$variable
was insteaddf$variable + 5
I would probably suggest that there is some earlier problem/bug that needs fixing.