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)
Related
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
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:
Appending a list to a list of lists in R
(7 answers)
Closed 6 years ago.
I have 1 list A containing 2 vectors and 1 vector B. I want to insert B into A such that B is in the first position.
I have this:
A <- list(LETTERS, letters)
B <- 1:10
I want this:
A <- list(B, A[[1]], A[[2]])
I currently do this:
B <- list(B)
for (i in 1:length(A)) {
B[i+1] <- A[[i]]}
Is there a better way without using for loops?
You can simply use c :
c(list(B), A)
This question already has answers here:
How to name variables on the fly?
(6 answers)
Closed 8 years ago.
I'd like to use the value of a variable as part of the name of an object to make the creation of new objects with names that I don't have to list out explicitly. An example:
Here is what I don't want to do, declare every single name for 10 objects going down the list from 1 to 10.
a=41:50
a1=a[1]
a2=a[2]
a3=a[3]
a4=a[4]
a5=a[5]
a6=a[6]....
Here is what would make more sense - just putting the name some how as "a" and then combined with the value of i in the loop. Does the same thing, creates 10 objects.
for(i in 1:10){
a#paste.in.the.value.of.i.somehow...=a[i]
}
Thanks for you help!
for(i in 1:10) {assign(paste0("a", i), a[i])}
a1
#[1] 41
a2
#[1] 42
a3
#[1] 43