This question already has answers here:
What is the idiomatic Rust way to copy/clone a vector in a parameterized function?
(2 answers)
Closed 5 years ago.
I was wondering how to create a new Vector from an old Vector, and insert elements into it.
let vec1 = vec!["Hello", "world!"];
let vec2 = Vec::newFrom(vec1).insert(1, " ");
What method/function can I use to accomplish this?
Use clone to make a copy of the original Vec.
let vec1 = vec!["Hello", "world!"];
let mut vec2 = vec1.clone();
vec2.insert(1, " ");
Related
This question already has answers here:
Is there some 'cross apply' function in R?
(3 answers)
Closed 2 months ago.
Firstly, sorry for the confusing title, I had no idea where to start with this!
I am looking at passing a string of characters into the find_thread_urls() function from RedditExtractoR. I want to save iterating through each of the 6 possible iterations in the below example code (which does not work).
Is there a neat way of doing this?
library(RedditExtractoR)
keywords <- c('cats', 'dogs', 'catnip')
subreddit <- c('cats', 'animals')
post_urls <- find_thread_urls(keywords = keywords,
subreddit = subreddit)
Not familiar with RedditExtractoR, but a generic solution to this kind of task is to use nested lapply() calls:
library(RedditExtractoR)
post_urls <- lapply(
setNames(keywords, keywords),
\(kw) lapply(
setNames(subreddit, subreddit),
\(sr) paste(kw, sr)
)
)
(The setNames() calls makes it so the output list is named based on the inputs.)
This question already has answers here:
R list from variables using variable names [duplicate]
(2 answers)
Can lists be created that name themselves based on input object names?
(4 answers)
Closed 4 years ago.
I've created objects (within a for loop) and summarize them in a list at the end of each loop.
in1 = which(var1 == 1)
in2 = which(var1 == 2)
out1= which(var2 == 1)
out2= which(var2 == 2)
templist = list(in1, in2, out1, out2)
Every object in the list has no name now. I know I could fix this by using:
templist = list(name1 = in1, name2 = in2, name3 = out1, name4 = out2)
But I was wondering if there is as possibility to make this more automatically, like "taking the object's name as name of the list's element". In my opinion this seems obvious but to R it doesn't seem so obvious. I am sure there is a good reason for that, but anyway: Is there a fancy solution to automatically use the object's name as the element's name within the list?
This question already has an answer here:
Duplicate list names in R
(1 answer)
Closed 5 years ago.
Hi suppose I have the following:
a <- list (a=55, a=66, c=100)
What I want to do is print only objects that is named a?
however when I do this, print (a$a) it will just print the first object 55,
I also tried looping through like a$a but that did not work as well.
for (b in a$a ){
print (b[1])
}
I could try to loop and do some sort of string comparison with the name but I'm planning to work this through a huge list in the 100's MB + so would like to avoid this. thanks!
If we need to print all the list elements that have name 'a', then create a logical vector with == and subset the list
a[names(a) == 'a']
This question already has answers here:
Append an object to a list in R in amortized constant time, O(1)?
(17 answers)
Closed 8 years ago.
I would like to add elements to a list in a loop (I don't know exactly how many)
Like this:
l <- list();
while(...)
l <- new_element(...);
At the end, l[1] would be my first element, l[2] my second and so on.
Do you know how to proceed?
You should not add to your list using c inside the loop, because that can result in very very slow code. Basically when you do c(l, new_element), the whole contents of the list are copied. Instead of that, you need to access the elements of the list by index. If you know how long your list is going to be, it's best to initialise it to this size using l <- vector("list", N). If you don't you can initialise it to have length equal to some large number (e.g if you have an upper bound on the number of iterations) and then just pick the non-NULL elements after the loop has finished. Anyway, the basic point is that you should have an index to keep track of the list element and add using that eg
i <- 1
while(...) {
l[[i]] <- new_element
i <- i + 1
}
For more info have a look at Patrick Burns' The R Inferno (Chapter 2).
The following adds elements to a vector in a loop.
l<-c()
i=1
while(i<100) {
b<-i
l<-c(l,b)
i=i+1
}
This question already has an answer here:
Can you pass a vector to a vararg?: Vector to sprintf
(1 answer)
Closed 9 years ago.
Say I have a function handed to me that I cannot change and must use as is. This function takes several objects in the form of
oldFunction( object1, object2, object3, ...)
where ... are other arguments. I want to write a wrapper to take a list of objects. My idea was this.
sjb.ListWrapper <- function(myList,...) {
lLen <- length(myList)
myStr <- ""
for( i in 1:lLen) {
myStr <- paste(myStr, "myList[[", i , "]],",sep="")
}
myCode <- paste("oldFunction(", myStr, "...)")
eval({myCode})
}
However, the issue is that I want to use this from Sweave and I need the output of oldFunction to be printed. What is the right way to do this?
Thanks.
You are looking for do.call:
f <- function(x,y,z)x+y+z
do.call(f,list(1,2,3))
[1] 6