This question already has answers here:
Create sequence of repeated values, in sequence?
(3 answers)
Closed 5 months ago.
Let's say that I have three letters: a,b,c .
How can I get the following sequence:
"a" "a" "a" "b" "b" "b" "c" "c" "c"
There are different ways to use the rep function to make sequences,
one way is with the each to repeat each element in a vector a number of times, and with the times argument, that let you repeat the whole vector a number of times.
Check both in usage here
rep(c('a', 'b', 'c'), each = 3)
#> [1] "a" "a" "a" "b" "b" "b" "c" "c" "c"
rep(c('a', 'b', 'c'), times = 3)
#> [1] "a" "b" "c" "a" "b" "c" "a" "b" "c"
Created on 2022-09-29 with reprex v2.0.2
Related
Let's say I have a simple vector x in R. It is in the order 'a','b','c','d'. Is there a function that would take the vector and reorder it with wrap around? For example, how can I get x to be 'c','d','a','b'?
#Vector x
> x <- letters[1:4]
> x
[1] "a" "b" "c" "d"
#What I want:
> somefcn(x, 3)
[1] "c" "d" "a" "b"
x <- letters[1:4]
shiftnum <- 3
c(x[shiftnum:length(x)],x[1:shiftnum-1])
[1] "c" "d" "a" "b"
Is a very rough way to do, but it works
I would like to make an array that summarises the rows of a data frame with the unique values contained within said rows.
with sample the following example code:
ref <- c(1:8)
data1 <- c("A","","C","","","","A","")
data2 <- c("A","","","A","C","","","")
data3 <- c("","B","","","","","","B")
data4 <- c("A","B","","","","D","A","")
initial.data <- data.frame(ref, data1, data2, data3, data4)
I can obtain what I want with:
summary.data <- paste(initial.data[,2], initial.data[,3],
initial.data[,4], initial.data[,5], sep='')
desired.data <- substring(summary.data,1,1)
However, I would like a more parsimonious way of coding this and one that does not assume that each row may only take one value.
You can try
apply(initial.data[-1],1, function(x) unique(x[x!='']))
#[1] "A" "B" "C" "A" "C" "D" "A" "B"
Or
substr(do.call(paste0, initial.data[-1]),1,1)
#[1] "A" "B" "C" "A" "C" "D" "A" "B"
Or use max.col
initial.data[cbind(1:nrow(initial.data),max.col(initial.data[-1]!='')+1)]
#[1] "A" "B" "C" "A" "C" "D" "A" "B"
I currently have an array of lists and a matrix that are produced with this code:
require(gtools)
FiveStates = array(list(NULL), c(32,2))
four.1 = combinations(5,4,c(LETTERS[1:5]))
four.2 = four.1[nrow(four.1):1,]
I'd like to replace rows 2-6 of the first column in the array FiveStates with all five rows in the matrix four.2 elementwise. How can I do this without having to replace each row separately?
Edit: I'd like to make FiveStates[2,1] show "B", "C", "D", "E"; FiveStates[3,1] show "A", "C", "D", "E"; and so on and so forth, so that the 2nd to 6th entries in the first column of FiveStates have vectors that match the rows of four.2[2:6,].
(Also, the package you need to use the combinations() function is now in the code. Sorry about that.)
You can replace blocks of a matrix that is populated with list using regular matrix indexing:
FiveStates[2:6, 1] <- lapply(1:nrow(four.1), function(x) four.2[x, ] )
FiveStates[2:6, 1]
[[1]]
[1] "B" "C" "D" "E"
[[2]]
[1] "A" "C" "D" "E"
[[3]]
[1] "A" "B" "D" "E"
[[4]]
[1] "A" "B" "C" "E"
[[5]]
[1] "A" "B" "C" "D"
In R,
I have a vector of 5 unique elements:
X<-c("A","B","C","D","E")
And a vector of repeated elements:
Y<- c("A","C","M","Z","B","C","C","R","V","D","D","B","A","V","E","E")
I want to obtain the position of elements of Y that a are in X becase Y are rownames of a matrix.
But Y[match(Y,X)] gives:
[1] "A" "M" NA NA "C" "M" "M" NA NA "Z" "Z" "C" "A" NA "B" "B"
The response should be:
c("A","C",NA,NA,"B","C","C",NA,NA,"D","D","B","A",NA,"E","E").
to select the rows:
Y[-which(is.na(Y[match(Y,X)]))]
Is there a better and more elegant alternative?
You can use %in%:
Y[Y %in% X]
[1] "A" "C" "B" "C" "C" "D" "D" "B" "A" "E" "E"
Does this help?
So this is a pretty odd question, but i have two vectors in R: one is a sequential list of IDs and the other is how many times I want that id to appear in the new vector.
for example:
ids: A B C D
times: 4 2 5 3
and i want to end up with
new: A A A A B B C C C C C D D D
Does this make sense?
Use rep function as in :
> rep(c('A', 'B', 'C', 'D'), times=c(4, 2, 5, 3))
[1] "A" "A" "A" "A" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D"
Use rep()
> rep(LETTERS[1:4], times = c(4,2,5,3))
[1] "A" "A" "A" "A" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D"
That certainly makes sense.
rep(c("A","B","C","D"),times=c(4,2,5,3))
[1] "A" "A" "A" "A" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D"