Displaying 20 results from an estimated 40000 matches similar to: "how to efficiently extract elements of a list?"
2009 Oct 25
3
NULL elements in lists ... a nightmare
I can define a list containing NULL elements:
> myList <- list("aaa",NULL,TRUE)
> names(myList) <- c("first","second","third")
> myList
$first
[1] "aaa"
$second
NULL
$third
[1] TRUE
> length(myList)
[1] 3
However, if I assign NULL to any of the list element then such
element is deleted from the list:
> myList$second <-
2004 Jan 15
3
Extracting multiple elements from a list
For a long time I've wanted a way to conveniently extract multiple elements
from a list, which [[ doesn't allow. Can anyone suggest an efficient
function to do this? Wouldn't it be a sensible addition to R?
For example,
alist <- list()
alist[[1]] <- list()
alist[[1]]$name <- "first"
alist[[1]]$vec <- 1:4
alist[[2]] <- list()
alist[[2]]$name <-
2008 Dec 24
3
Extract values based on indexes without looping?
Dear R-Helpers:
I am a entry level user of R.
Have the following question. Many thanks in advance.
# value.vec stores values
value.vec <- c('a','b','c')
# which.vec stores the locations/indexs of values in value.vec.
which.vec <- c(3, 2, 2, 1)
# How can I obtain the following vector based on the value.vec and which.vec
mentioned above
# vector.I.want <-
2004 Dec 21
3
R code for var-cov matrix given variances and correlations
Dear list members,
Where can I find code for computing the p*p variance-covariance
matrix given a vector of p variances (ordered varA, varB, ...,
varp) and a vector of all possible correlations (ordered corAB,
corAC, ..., corp-1,p)?
I know that the covariance between 2 variables is equal to the
product of their correlation and their standard deviations:
corAB * varA^.5 * varB^.5
and so:
2011 Feb 11
3
How can we make a vector call a function element-wise efficiently?
Hello
I have a time-comsuming program which need to simplify, I have tested the annotated program as follow:
> #define function which will be call
> calsta <- function(c, n=100000)
+ {
+ i <- seq(from=0, length=c)
+ logx <- lchoose(NT-n, CT-i) + lchoose(n, i)
+ logmax <- max(logx)
+ logmax + log(sum(exp(logx - logmax)))
+ }
> CT=6000 #assignment to CT
>
2008 Sep 24
1
splitting strings efficiently
I have a very long list of strings. Each string actually contains multiple
values separated by a semi-colon. I need to turn each string into a vector
of the values delimited by the semi-colons. I know I can do this very
laboriously by using loops, nchar, and substr, but it is terribly slow. Is
there a basic R function that handles this situation? If not, is there
perhaps a faster way to do it than
2013 Mar 08
2
Unexpected behaviour of apply()
Hello everyone,
Considering the following code sample :
----
indexes <- function(vec) {
vec <- which(vec==TRUE)
return(vec)
}
mat <- matrix(FALSE, nrow=10, ncol=10)
mat[1,3] <- mat[3,1] <- TRUE
----
Issuing apply(mat, 1, indexes) returns a 10-cell list, as expected.
Now if I do:
----
mat[1,3] <- mat[3,1] <- FALSE
apply(mat, 1, indexes)
----
I would expect a
2006 Mar 02
1
Curious subsetting behavior
I have a simple vector, called tmp that I want to subset based on another
vector called vec. Everything works as expected except for below where the
subsetting returns something other than the original data. Any ideas?
> vec <- c(1,2,3,4,5,59,60,27,32,21)
> tmp
[1] 1.0 1.1 2.0 2.1 2.2 3.0 3.1 4.0 5.0 5.1 6.0 7.0 8.0 8.1
9.0
[16] 9.1 9.2 10.0 10.1 11.0 12.0 13.0 14.0
2002 Sep 09
2
lapply-related question
Dear R-gurus,
I would like to use a lapply on a kind of "bivariate" problem. I have
a vector and a list, components of which are vectors, e.g.
vec <- c(1,2,3)
lst <- list(1, c(2,3), c(4,5,6))
I want to apply a function to each component of the list, using the
corresponding component of the vector as a parameter. E.g. I want a
list in the form
list(lst[[1]] + vec[1], lst[[2]]
2006 Apr 04
1
Indexing a vector by a list of vectors
Hello R-help -
I have
vec <- c("string1", "string2", "string3")
ind <- list(c(1,2),c(1,2,3))
I want "vec" indexed by each vector in the list "ind".
The first element of the list I want would be vec[c(1,2)],
the second element would be vec[c(1,2,3)], like the following.
[[1]]
[1] "string1" "string2"
[[2]]
[1]
2011 Sep 26
4
Testing for arguments in a function
I don't understand how this function can subset by i when i is missing....
## My function:
myfun = function(vec, i){
ret = vec[i]
ret
}
## My data:
i = 10
vec = 1:100
## Expected input and behavior:
myfun(vec, i)
## Missing an argument, but error is not caught!
## How is subsetting even possible here???
myfun(vec)
Is there a way to check for missing function arguments, *and*
2011 Apr 19
2
Simple question
I am trying to convert a string to a vector, and I'm stuck!
Suppose you have a string of numbers (string) that you want to convert to a
vector (vec). I am able to split the string turning it into a list by using
strsplit, but this makes a list, which I can't seem to access the way that
I'd like to (it becomes a character list of only one(?!?) element.)
2007 Sep 03
3
element wise opertation between a vector and a list
I want to try to get a result of element wise addition between a
vector and a list. It can be done with "for statement." In order to
reducing computing time, I have tried to avoid "for state." If anybody
give me an idea, I would apprecite it much.
for example, with a & b as below lines,
a<- list(c(1,3),c(1,2),c(2,3))
b<-c(10,20,30)
I would like to have a list (like
2011 Aug 12
1
Details of subassignment (for vectors and data frames)
Hi All:
I'm looking to find out a bit more about how subassignment actually works and am hoping someone with knowledge of the details can fill me in (I've looked at the source code, but my knowledge of C is lacking).
In the case of vectors, my reading of ?"[" would indicate that for a vector, vec <- 1:25, vec[c(1,5,25)] <- c(101,102,103)is functionally the same as
2001 Nov 12
2
[Q] Setting elements of a vector
Hi!
I'm new to R and have problems with the following code:
<code>
awprob<-c(0.4,0.4,0.1,0.1)
awsample<-sample(c(1:4),3,awprob,replace=TRUE)
vec<-vector(mode="numeric",10000)
vec[]<-sum(sample(c(1:4),20,awprob,replace=TRUE))
</code>
My problem: The elements of vec have the same value. How
can I tell R that I want to have a *new* sample for each value
and
2008 Dec 23
1
quotation problem/dataframe names as function input argument.
Dear R friends:
Can someone help me with the following problem? Many thanks in advance.
# Problem Description:
# I want to write functions which take a (character) vector of dataframe
names as input argument.
# For example, I want to extract the number of observations from a number of
dataframes.
# I tried the following:
nobs.fun <- function (dframe.vec)
{
nobs.vec <-
2009 Dec 05
4
paste adjacent elements matching string
Hi all,
I would like to combine elements of a vector:
vec <- c("astring", "b", "cstring", "d", "e")
> vec
[1] "astring" "b" "cstring" "d" "e"
such that for every element that contains "string" at the end, it is
combined with the next element, so that I get this:
2012 May 31
1
Warning message: numerical expression has 1000 elements: only the first used
Hi,
Your mistake seems to be in
sum(v[1:x])
You create "x" as a vector but your treat it as a single number.
v[1:x] expects "x" to be a single number and only considers its first
element which is 1.
If I understand your query correctly, the following might handle your
problem:
sum.vec <-NULL
for (x in 1:1000){
t <- rbinom(1000, 1, 0.5)
v <- replace(t,t==0,-1)
2009 Dec 11
4
extracting vectors from lists of lists
Good evening
I often have as output from simulations a list of various values, vectors
and matrices.
Supposing that I then run said simulation several times, I often want to
extract a particular result from each simulation for plotting and,
ideally, put it in a matrix.
A simple example
v1 <- 1:5
v2 <- 6:10
other1 <- "stuff"
other2 <- "stuff"
set1 <-
2009 Jul 05
2
From "list" to "matrix"
Suppose, I have following
vec <- vector("list", length=3)
for (i in 1:3) vec[[i]] <- matrix(rnorm((i+3)*2), (i+3))
vec
Now I want to change the type of "vec" from list to a matrix with (4+5+6)
rows and 2 columns. How can I do that?
Thanks and regards,
--
View this message in context: http://www.nabble.com/From-%22list%22-to-%22matrix%22-tp24345922p24345922.html
Sent