This question already has an answer here:
rep() with each equals a vector
(1 answer)
Closed 3 years ago.
I'm banging my head against the wall for this. It must be so easy, but in my 7+ years of R coding I never had to do this, and I can't figure out how to do it now.
Here's a toy df:
> x <- c(1,2,3)
> y <- c(2,3,4)
> TRY <- as.data.frame(x,y)
I want to see a brand new vector (in a df or just as a vector, whatever) that has the value "1" repeated 2 times, and then "2" repeated 3 times, and then "3" repeated 4 times.
That's all I want. Can someone PLEASE help me?
I thought of melt and cast and all that, but I don't think they're what I need here.
Thanks!
Divya
Adding comment as answer to help future users.
You can get this effect using rep like so:
x <- c(1,2,3)
y <- c(2,3,4)
vec <- rep(x, y)
vec
# [1] 1 1 2 2 2 3 3 3 3
Related
This question already has answers here:
Filtering single-column data frames
(1 answer)
How to subset matrix to one column, maintain matrix data type, maintain row/column names?
(1 answer)
Closed 4 months ago.
I have a large matrix from which I want to select rows to further process them.
Ultimatly, I want to convert the matrix to a dataframe that only contains my rows of interest.
The easiest thing would be to simple run something like data.frame(my_matrix)[c(3,5),]. The problem with this is, that I am working with a very big sparse matrix, so converting all of it to a dataframe just to select a few rows is ineffective.
This option does what I want, but somehow only returns the result that I intend if I indicate at least 2 indices.
m <- matrix(1:25,nrow = 5)
rownames(m) <- 1:5
colnames(m) <- c("A","B","C","D","E")
data.frame(m[c(3,5),])
If i only want to select 1 row, and if I use the code above, the result is not a "wide" dataframe, but instead a long one, which looks like this:
data.frame(m[c(2),])
m.c.2....
A 2
B 7
C 12
D 17
E 22
Is there a simple way to get a dataframe with just one row out of the matrix without converting the whole matrix first? It feels like I am overlooking something very obvious here...
Any help is much appreciated!
You need to use drop=FALSE in the matrix subset, otherwise it will turn the matrix into a vector, as you saw.
m <- matrix(1:25,nrow = 5)
rownames(m) <- 1:5
colnames(m) <- c("A","B","C","D","E")
data.frame(m[c(2),, drop=FALSE])
#> A B C D E
#> 2 2 7 12 17 22
Created on 2022-10-12 by the reprex package (v2.0.1)
This question already has answers here:
Check to see if a value is within a range?
(7 answers)
Closed 5 years ago.
I'm starting to learn R, as it's needed for work. I have never done statistical work, so I'm a bit lost.
I'm looking to get the value of x between two numbers.
So, for example, the range is 3:7 I need to print 4,5,6
I have tried
x <- 3:7
x[x>3 && x<7]
and
x <- 3
v <- 7
cbind(x, findInterval(x, v))
Any advice/guidelines
An option is between from data.table
x[data.table::between(x, 3, 7, incbounds = FALSE)]
#[1] 4 5 6
This question already has answers here:
How to tell what is in one vector and not another?
(6 answers)
Closed 5 years ago.
I try to do this in R:(for example)
let x = c(1,2,3,4,5,6,7,8) and y=c(1,2,8)
So
x[x!=y] = numeric(0) ????
I want to get as a result 3,4,5,6,7
Is there a practical way to do this?
Thanks
Use value matching %in% and remove the elements of x that are present in y
x[-which(x %in% y)]
#[1] 3 4 5 6 7
This question already has answers here:
Adding all elements of two lists
(2 answers)
Closed 6 years ago.
I am gonna add each element of one list to another in parallel (a.k.a, where element of list_1 are added to element of list_2 in parallel). I used lapply for this but could not get right one. I bet there is must be easy way to do this.
this is my reproducible example:
list_1 <- list('a'=c(1,1,1), 'b'=c(1,1,1,1))
list_2 <- list('a_'=c(1,0,0), 'b_'=c(1,1,0,0))
my desired output(just manually sketch my expected output like this):
output <- list('a'= c(2,1,1), 'b'=c(2,2,1,1))
How can I get this result? please help. Thanks a lot
Here is how to do it,
mapply('+', list_1, list_2)
#$a
#[1] 2 1 1
#$b
#[1] 2 2 1 1
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
R: How to convert string to variable name?
If I do:
'a' = c(1:10)
a
[1] 1 2 3 4 5 6 7 8 9 10
here I assign a vector to a string (variable)
but i need to a do something like:
a = 'c10'
and then
a = c(1:10)
but the last a must to be c10
How can i do it?
Not sure what you're looking for but your first assignment doesn't need the c() and doesn't need quotes around the a.
a <- 1:10
if you want the last entry to be the string 'c10', you can get there a few different ways.
a <- c(1:9,'c10')
or
a <- 1:10
a[10] <- 'c10'
Or if Ben Bolker is on the right track:
a <- 'c10'
assign(a,1:10)