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
Related
This question already has answers here:
Access variable value where the name of variable is stored in a string
(4 answers)
Closed 2 years ago.
I want to refer to a data frame by using a character vector.
I believe the simple example below illustrates the problem.
# I have a data frame called A
A <- c(1, 2, 3, 4)
# I have a character vector called B, containing the character "A"
B <- "A"
# Now I want a third vector (C) to get the content of A, simply by referring to vector B
# Obviously, I cannot write
C <- B
# ... as this would give me
[1] "A"
# ... and NOT what I want:
[1] 1 2 3 4
How do I use a character vector to refer to the name and thus the content of an existing data frame?
PS.
I have been made aware of my questiong being a duplicate. But since the wordings are different, I didn't find the other post when searching online:
Access variable value where the name of variable is stored in a string
I keep my post, as others too might fail to find the earlier one.
It would be get to return the value of the object name as string
C <- get(B)
If there are more than objects, use mget to return the values in a list
This question already has answers here:
Split data.frame based on levels of a factor into new data.frames
(3 answers)
How to create dataframe subsets split by week? [duplicate]
Closed 3 years ago.
I have data that is like the following (Except that is populated by all 52 weeks). "data"
Week State Value
1 CA 4
1 AL 5
1 MO 9
1 IL 12
2 NY 1
2 CA 2
2 WA 8
3 WY 9
3 SC 10
I would like to create new data.frames for every week so that I may do further analyses. How would I go about this? I tried:
for( i in 1:52) {
DataWeeki <- data[which(data$Week==i),]
}
This creates a single dataframe of 3 variables (with the appropriate variable names) but no data.
What is the correct way to go about this?
We can use split in base R to create a list of data.frames
lst1 <- split(data, data$Week)
names(lst1) <- paste0("DataWeek", 1:52)
From the list, elements can be extracted with $ or [[
lst1[["DataWeek1"]]
It is not recommended to create multiple objects in the global environment, but if we need, we can use list2env (but there would be 52 objects and not good at all)
list2env(lst1, .GlobalEnv)
Regarding why it creates a single object is because the object gets updated in each run and what we get is the last run object. Instead, here assign is needed (not recommended)
for( i in 1:52) {
assign(paste0("DataWeek", i), data[which(data$Week==i),])
}
lists can also be created with for loop
lst1 <- vector("list", 52)
for(i in 1:52) {
lst1[[i]] <- data[which(data$Week==i),]
}
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:
R grep: is there an AND operator?
(4 answers)
Closed 8 years ago.
So if I have the following
list <- c("catdog","chicken","poop")
names <- c("Fabio","John","Jack")
df <- data.frame(names, list, stringsAsFactors=FALSE)
names list
1 Fabio catdog
2 John cat
3 Jack dog
Assuming list is a column of strings. I want to know how can I return rows where "cat" AND "dog" after appearing once as a pair they may appear more times. I tried:
want <- c("cat","dog")
df[grepl(paste(want,collapse="&"),df$list),]
I know this works with "|" for some reason its not working with "&". Let me know if anyone can help with this. Thanks!
This is an option, if neither 'cat' nor 'dog' can repeat within a single string.
df[grepl('(cat)|(dog).*(\\1|\\2)', df$list), ]
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)