How do I force my vector to split into arguments? - r

I'm having problems with applying a function to a vector of arguments. The point is, none of the arguments are vectors.
I'm trying to apply my function with the do.call command, and my attempts go like this:
do.call("bezmulti", list(dat$t, as.list(getvarnames(n, "a"))))
where bezmulti is a function that takes in a vector (dat$t) and an indefinite number of single numbers, which are provided by the function getvarnames in the form of a vector, which I need to split.
The problem is that this list doesn't work the way I want it to - the way I would want would be:
[[1]]
#vector goes here
[[2]]
#the
[[3]]
#numbers
[[4]]
#go
[[5]]
#here
however my proposed solution, and all my other solutions provide lists that are either somehow nested or have only two elements, both of which are vectors. Is there a way to force the list to be in the format above?
EDIT: Functions used in this post look as follows
bezmulti <- function(t,...) {
coeff <- list(...)
n <- length(coeff)-1
sumco <- rep(0, length(t))
for (i in c(0:n)) {
sumco=sumco+coeff[[i+1]]*choose(n, i)*(1-t)^(n-i)*t^i
}
return(sumco)
}
getvarnames <- function(n, charasd) {
vec=NULL
for (j in c(1:n)) {
vec <- append(vec, eval(str2expression(paste0(charasd, as.character(j)))))
}
return(vec)
}

I think what you need to do is this:
do.call("bezmulti", c(list(dat$t), as.list(getvarnames(n, "a"))))
For example:
dat= data.frame(t = c(1,2,3,4,6))
c(list(dat$t), as.list(c(8,10,12)))
Output:
[[1]]
[1] 1 2 3 4 6
[[2]]
[1] 8
[[3]]
[1] 10
[[4]]
[1] 12

Related

R: Repeating elements of a list for usage in purrr::pmap()

This is just a "cosmetical" thing, but I just can't get it to work. :(
I have a nested list that I want to pass to pmap(). Some elements are repeated within the sublists. I'm looking for an elegant way to repeat elements multiple times within a sublist.
Simplified example with less elements:
arg_list <- list(url = x,
category = c("/activity",
"/current",
"/desktop",
"/login/user",
"/message",
"/forum"),
action = list(c("ACTIVITY_RESUME", "ACTIVITY_PAUSE"),
c("FRAGMENT_RESUME", "FRAGMENT_PAUSE"),
c("FRAGMENT_RESUME", "FRAGMENT_PAUSE"),
c("onResume", "onPause"),
c("onResume", "onPause"),
c("onResume", "onPause"))
get_durations <- function(x, category, action) { ... }
durations <- pmap(.l = arg_list, safely(get_durations))
For example, I want to repeat c("onResume", "onPause") three times, instead of typing it by hand.
I tried various variations of rep(list(c("onResume", "onPause")), times = 3) but I wasn't able to extract the elements as neccessary.
Thank you! :)
rep(list(c("onResume", "onPause")), times = 3) should work. What seems to be issue?
If you need to repeat different vectors a different number of times, you can use map2 (Note the usage of flatten to convert the final list-of-lists to a list of vectors):
map2( list(c("ACTIVITY_RESUME", "ACTIVITY_PAUSE"),
c("FRAGMENT_RESUME", "FRAGMENT_PAUSE"),
c("onResume", "onPause")),
1:3, ~rep(list(.x), .y) ) %>% purrr::flatten()
# [[1]]
# [1] "ACTIVITY_RESUME" "ACTIVITY_PAUSE"
#
# [[2]]
# [1] "FRAGMENT_RESUME" "FRAGMENT_PAUSE"
#
# [[3]]
# [1] "FRAGMENT_RESUME" "FRAGMENT_PAUSE"
#
# [[4]]
# [1] "onResume" "onPause"
#
# [[5]]
# [1] "onResume" "onPause"
#
# [[6]]
# [1] "onResume" "onPause"

R - Naming multiple functions in a for loop

I have the following data frame:
> coc_comp_model[1:3,]
Relationship Output Input |r-Value| Y-Intercept Gradient
1 DG-r ~ DG-cl DG-r DG-cl 0.8271167 0.0027217513 12.9901380
2 CA3-r ~ CA3-cl CA3-r CA3-cl 0.7461309 0.0350767684 27.6107963
3 CA2-r ~ CA2-cl CA2-r CA2-cl 0.9732584 -0.0040992226 35.8299582
I want to create simple functions for each row of the data frame. here's what I've tried:
for(i in 1:nrow(coc_comp_model)) {
coc_glm_f[i] <- function(x)
x*coc_comp_model$Gradient[i] + coc_comp_model$Y-Intercept[i]
}
also tried making a vector of functions, which also does ont work either.
Thanks for reading this/helping.
Something like this:
myfunc<-function(datrow, x){
x*as.numeric(datrow[6]) + as.numeric(datrow[5] )
}
Then you can use apply to call it on each row, changing x as desired:
apply(hzdata, 1, myfunc, x = 0.5)
note: using dput() to share your data is much easier than a pasting in a subset.
There is no such thing as a vector of functions. There are 6 atomic vector types in R: raw, logical, integer, double, complex, and character, plus there is the heterogeneous list type, and finally there is the lesser known expression type, which is basically a vector of parse trees (such as you get from a call to the substitute() function). Those are all the vector types in R.
printAndType <- function(x) { print(x); typeof(x); };
printAndType(as.raw(1:3));
## [1] 01 02 03
## [1] "raw"
printAndType(c(T,F));
## [1] TRUE FALSE
## [1] "logical"
printAndType(1:3);
## [1] 1 2 3
## [1] "integer"
printAndType(as.double(1:3));
## [1] 1 2 3
## [1] "double"
printAndType(c(1i,2i,3i));
## [1] 0+1i 0+2i 0+3i
## [1] "complex"
printAndType(letters[1:3]);
## [1] "a" "b" "c"
## [1] "character"
printAndType(list(c(T,F),1:3,letters[1:3]));
## [[1]]
## [1] TRUE FALSE
##
## [[2]]
## [1] 1 2 3
##
## [[3]]
## [1] "a" "b" "c"
##
## [1] "list"
printAndType(expression(a+1,sum(1,2+3*4),if (T) 1 else 2));
## expression(a + 1, sum(1, 2 + 3 * 4), if (T) 1 else 2)
## [1] "expression"
If you want to store multiple functions in a single object, you have to use a list, and you must use the double-bracket indexing operator in the lvalue to assign to it:
fl <- list();
for (i in 1:3) fl[[i]] <- (function(i) { force(i); function(a) a+i; })(i);
fl;
## [[1]]
## function (a)
## a + i
## <environment: 0x600da11a0>
##
## [[2]]
## function (a)
## a + i
## <environment: 0x600da1ab0>
##
## [[3]]
## function (a)
## a + i
## <environment: 0x600da23f8>
sapply(fl,function(f) environment(f)$i);
## [1] 1 2 3
sapply(fl,function(f) f(3));
## [1] 4 5 6
In the above code I also demonstrate the proper way to closure around a loop variable. This requires creating a temporary function evaluation environment to hold a copy of i, and the returned function will then closure around that evaluation environment so that it can access the iteration-specific i. This holds true for other languages that support dynamic functions and closures, such as JavaScript. In R there is an additional requirement of forcing the promise to be resolved via force(), otherwise, for each generated function independently, the promise wouldn't be resolved until the first evaluation of that particular generated function, which would at that time lock in the current value of the promise target (the global i variable in this case) for that particular generated function. It should also be mentioned that this is an extremely wasteful design, to generate a temporary function for every iteration and evaluate it, which generates a new evaluation environment with a copy of the loop variable.
If you wanted to use this design then your code would become:
coc_glm_f <- list();
for (i in 1:nrow(coc_comp_model)) {
coc_glm_f[[i]] <- (function(i) { force(i); function(x) x*coc_comp_model$Gradient[i] + coc_comp_model$`Y-Intercept`[i]; })(i);
};
However, it probably doesn't make sense to create a separate function for every row of the data.frame. If you intended the x parameter to take a scalar value (by which I mean a one-element vector), then you can define the function as follows:
coc_glm_f <- function(x) x*coc_comp_model$Gradient + coc_comp_model$`Y-Intercept`;
This function is vectorized, meaning you can pass a vector for x, where each element of x would correspond to a row of coc_comp_model. For example:
coc_comp_model <- data.frame(Relationship=c('DG-r ~ DG-cl','CA3-r ~ CA3-cl','CA2-r ~ CA2-cl'),Output=c('DG-r','CA3-r','CA2-r'),Input=c('DG-cl','CA3-cl','CA2-cl'),`|r-Value|`=c(0.8271167,0.7461309,0.9732584),`Y-Intercept`=c(0.0027217513,0.0350767684,-0.0040992226),Gradient=c(12.9901380,27.6107963,35.8299582),check.names=F);
coc_glm_f(seq_len(nrow(coc_comp_model)));
## [1] 12.99286 55.25667 107.48578

subset() drops attributes on vectors; how to maintain/persist them?

Let's say I have a vector where I've set a few attributes:
vec <- sample(50:100,1000, replace=TRUE)
attr(vec, "someattr") <- "Hello World"
When I subset the vector, the attributes are dropped. For example:
tmp.vec <- vec[which(vec > 80)]
attributes(tmp.vec) # Now NULL
Is there a way to, subset and persist attributes without having to save them to another temporary object?
Bonus: Where would one find documentation of this behaviour?
I would write a method for [ or subset() (depending on how you are subsetting) and arrange for that to preserve the attributes. That would need a "class" attribute also adding to your vector so that dispatch occurs.
vec <- 1:10
attr(vec, "someattr") <- "Hello World"
class(vec) <- "foo"
At this point, subsetting removes attributes:
> vec[1:5]
[1] 1 2 3 4 5
If we add a method [.foo we can preserve the attributes:
`[.foo` <- function(x, i, ...) {
attrs <- attributes(x)
out <- unclass(x)
out <- out[i]
attributes(out) <- attrs
out
}
Now the desired behaviour is preserved
> vec[1:5]
[1] 1 2 3 4 5
attr(,"someattr")
[1] "Hello World"
attr(,"class")
[1] "foo"
And the answer to the bonus question:
From ?"[" in the details section:
Subsetting (except by an empty index) will drop all attributes except names, dim and dimnames.
Thanks to a similar answer to my question #G. Grothendieck, you can use collapse::fsubset see here.
library(collapse)
#tmp_vec <- fsubset(vec, vec > 80)
tmp_vec <- sbt(vec, vec > 80) # Shortcut for fsubset
attributes(tmp_vec)
# $someattr
# [1] "Hello World"

Access atomic vectors shown by Filter(is.atomic, eq) in R

Filter(is.atomic, something)
returns atomic vectors.
1. Weather -example here
> Filter(is.atomic, study)
$region
[1] "Hamburg" "Bremen"
2. mosaic-plot-as-tree-plot -example here
> Map(function(x) Filter(is.atomic, x), ls())
$g
[1] "g"
$lookup
[1] "lookup"
$req.data
[1] "req.data"
$tmp
[1] "tmp"
$tmp1
[1] "tmp1"
Look their positions can be arbitrary, I may have faintest clue of their data-structure so cannot use var$some$...$vector. I feel the need of ?Position. Use your imagination, the examples are not exclusive. How can I access their atomic vectors?
To flatten a list so you can access the atomic vectors, you can use following function:
flatten.list <- function(x){
y <- list()
while(is.list(x)){
id <- sapply(x,is.atomic)
y <- c(y,x[id])
x <- unlist(x[!id],recursive=FALSE)
}
y
}
This function maintains names of the elements. Usage, using the list x from Vincent's answer :
x <- list(
list(1:3, 4:6),
7:8,
list( list( list(9:11, 12:15), 16:20 ), 21:24 )
)
then:
> flatten.list(x)
[[1]]
[1] 7 8
[[2]]
[1] 1 2 3
[[3]]
[1] 4 5 6
[[4]]
[1] 21 22 23 24
...
To recursively do an action on all atomic elements in a list, use rapply() (which is what Vincent handcoded basically).
> rapply(x,sum)
[1] 6 15 15 30 54 90 90
> rapply(x,sum,how='list')
[[1]]
[[1]][[1]]
[1] 6
[[1]][[2]]
[1] 15
[[2]]
[1] 15
...
See also ?rapply
PS : Your code Map(function(x) Filter(is.atomic, x), ls()) doesn't make sense. ls() returns a character vector, so every element of that character vector will be returned as part of the list. This doesn't tell you anything at all.
Next to that, Filter() doesn't do what you believe it does. Taking the example list x, from the answer of Vincent, accessing only the atomic parts of it is pretty easy. Filter() only returns the second element. That's the only atomic element. Filter(is.atomic, x) is 100% equivalent to:
ind <- sapply(x, is.atomic)
x[ind]
Your question is very unclear, to say the least: an example of the input data you have and the desired output would help...
Since you suggest that we "use our imagination", I assume that you have a hierarchical data structure, i.e., a list of lists of...of lists, whose depth is unknown. For instance,
x <- list(
list(1:3, 4:6),
7:8,
list( list( list(9:11, 12:15), 16:20 ), 21:24 )
)
The leaves are vectors, and you want to do "something" with those vectors.
For instance, you may want to concatenate them into a single vector: that is what the unlist function does.
unlist(x)
You could also want all the leaves, in a list, i.e., a list of vectors.
You can easily write a (recursive) function that explores the data structure and progressively builds that list, as follows.
leaves <- function(u) {
if( is.atomic(u) ) { return( list(u) ) }
result <- list()
for(e in u) {
result <- append( result, leaves(e) )
}
return(result)
}
leaves(x)
You could also want to apply a function to all the leaves, while preserving the structure of the data.
happly <- function(u, f, ...) {
if(is.atomic(u)) { return(f(u,...)) }
result <- lapply(u, function(v) NULL) # List of NULLs, with the same names
for(i in seq_along(u)) {
result[[i]] <- happly( u[[i]], f, ... )
}
return( result )
}
happly(x, range) # Apply the "range" function to all the leaves
Filter will return a list. The functions lapply and sapply are typically used to process individual elements of a list object. If you instead want to access them by number using "[" or "[[" then you can determine the range of acceptable numbers with length(object). So object[[length(object)]] would get you the last element (as would ( tail(object, 1) ).

R search variable in all list objects [duplicate]

Filter(is.atomic, something)
returns atomic vectors.
1. Weather -example here
> Filter(is.atomic, study)
$region
[1] "Hamburg" "Bremen"
2. mosaic-plot-as-tree-plot -example here
> Map(function(x) Filter(is.atomic, x), ls())
$g
[1] "g"
$lookup
[1] "lookup"
$req.data
[1] "req.data"
$tmp
[1] "tmp"
$tmp1
[1] "tmp1"
Look their positions can be arbitrary, I may have faintest clue of their data-structure so cannot use var$some$...$vector. I feel the need of ?Position. Use your imagination, the examples are not exclusive. How can I access their atomic vectors?
To flatten a list so you can access the atomic vectors, you can use following function:
flatten.list <- function(x){
y <- list()
while(is.list(x)){
id <- sapply(x,is.atomic)
y <- c(y,x[id])
x <- unlist(x[!id],recursive=FALSE)
}
y
}
This function maintains names of the elements. Usage, using the list x from Vincent's answer :
x <- list(
list(1:3, 4:6),
7:8,
list( list( list(9:11, 12:15), 16:20 ), 21:24 )
)
then:
> flatten.list(x)
[[1]]
[1] 7 8
[[2]]
[1] 1 2 3
[[3]]
[1] 4 5 6
[[4]]
[1] 21 22 23 24
...
To recursively do an action on all atomic elements in a list, use rapply() (which is what Vincent handcoded basically).
> rapply(x,sum)
[1] 6 15 15 30 54 90 90
> rapply(x,sum,how='list')
[[1]]
[[1]][[1]]
[1] 6
[[1]][[2]]
[1] 15
[[2]]
[1] 15
...
See also ?rapply
PS : Your code Map(function(x) Filter(is.atomic, x), ls()) doesn't make sense. ls() returns a character vector, so every element of that character vector will be returned as part of the list. This doesn't tell you anything at all.
Next to that, Filter() doesn't do what you believe it does. Taking the example list x, from the answer of Vincent, accessing only the atomic parts of it is pretty easy. Filter() only returns the second element. That's the only atomic element. Filter(is.atomic, x) is 100% equivalent to:
ind <- sapply(x, is.atomic)
x[ind]
Your question is very unclear, to say the least: an example of the input data you have and the desired output would help...
Since you suggest that we "use our imagination", I assume that you have a hierarchical data structure, i.e., a list of lists of...of lists, whose depth is unknown. For instance,
x <- list(
list(1:3, 4:6),
7:8,
list( list( list(9:11, 12:15), 16:20 ), 21:24 )
)
The leaves are vectors, and you want to do "something" with those vectors.
For instance, you may want to concatenate them into a single vector: that is what the unlist function does.
unlist(x)
You could also want all the leaves, in a list, i.e., a list of vectors.
You can easily write a (recursive) function that explores the data structure and progressively builds that list, as follows.
leaves <- function(u) {
if( is.atomic(u) ) { return( list(u) ) }
result <- list()
for(e in u) {
result <- append( result, leaves(e) )
}
return(result)
}
leaves(x)
You could also want to apply a function to all the leaves, while preserving the structure of the data.
happly <- function(u, f, ...) {
if(is.atomic(u)) { return(f(u,...)) }
result <- lapply(u, function(v) NULL) # List of NULLs, with the same names
for(i in seq_along(u)) {
result[[i]] <- happly( u[[i]], f, ... )
}
return( result )
}
happly(x, range) # Apply the "range" function to all the leaves
Filter will return a list. The functions lapply and sapply are typically used to process individual elements of a list object. If you instead want to access them by number using "[" or "[[" then you can determine the range of acceptable numbers with length(object). So object[[length(object)]] would get you the last element (as would ( tail(object, 1) ).

Resources