Displaying 20 results from an estimated 2000 matches similar to: "lapply, if statement and concatenating to a list"
2010 Feb 26
3
Preserving lists in a function
Dear R users,
A co-worker and I are writing a function to facilitate graph plotting in R. The function makes use of a lot of lists in its defaults.
However, we discovered that R does not necessarily preserve the defaults if we were to input them in the form of list() when initializing the function. For example, if you feed the function codes below into R:
myfunction=function(
list1=list
2007 May 16
2
substitute "x" for "pattern" in a list, while preservign list "structure". lapply, gsub, list...?
I am experimenting with some of the common r functions.
I had a question re:using "gsub" (or some similar functions) on the contents of a list.
I want to design a function that looks at "everything" contained din a list, and anytime it finds the text string "pattern" replace it with "x". I also wish to preserve the "structure" of the original
2010 Mar 15
1
rbind, data.frame, classes
Hi,
This has bugged me for a bit. First question is how to keep classes with
rbind, and second question is how to properly return vecotrs instead of
lists after turning an rbind of lists into a data.frame
list1=list(a=2, b=as.Date("20090102", format="%Y%m%d"))
list2=list(a=2, b=as.Date("20090102", format="%Y%m%d"))
rbind(list1, list2) #this loses the
2011 Nov 21
1
Creating a list from all combinations of two lists
R-helpers:
Say I have two lists of arbitrary elements, e.g.:
list1=list(c(1:3),"R is fun!",c(3:6))
list2=list(c(10:5),c(5:3),c(13,5),"I am so confused")
I would like to produce a single new list that is composed of all
combinations of the "top level" of list1 and list2, e.g.:
listcombo=list(list(list1[[1]],list2[[1]]),list(list1[[1]],list2[[2]]
2012 Apr 19
1
question about lists
I am new to R, and I have been running into the following situation
when I mistype a variable name in some code:
> list1 <- list( a=1, b=2 )
> list2 <- list( a=1 )
> list2$b <- list1$c
> list2
$a
[1] 1
I would think at the point where I am trying to reference a field
called "c" -- that does not exist -- in list1, there would be an error
flagged.
Instead, list1$c
2011 Feb 06
1
Applying 'cbind/rbind' among different list object
Hi, I am wondering whether we can apply 'cbind/rbind' on many **equivalent**
list objects. For example please consider following:
> list1 <- list2 <- vector("list", length=2); names(list1) <- names(list2)
<- c("a", "b")
> list1[[1]] <- matrix(1:25, 5)
> list1[[2]] <- matrix(2:26, 5)
> list2[[1]] <- 10:14
> list2[[2]] <-
2004 Oct 25
1
usage and behavior of 'setIs'
Hello,
am I using 'setIs' in the correct way in the subsequent (artifical) example?
Do I have to specify explicit 'setAs' for 'list' and 'vector' or
should this work automatically, since "getClass("List1")" states
an explicit coerce also for these classes.
I'm working with R 2.0.0 Patched (2004-10-06) on windows 2000.
Thanks for your
2007 Oct 15
1
The "condition has length > 1" issue for lists
I have the following code:
list1 <- list()
for (i in list.files(pattern="filename1")){
x <- read.table(i)
list1[[i]] <- x
}
list2 <- list()
for (i in list.files(pattern="filename2*")){
x <- read.table(i)
list2[[i]] <- x
}
anslist <- vector('list', length(list1))
for(i in 1:length(list1))
if (list1[[i]] & list2[[i]] >1)
2011 Jan 08
3
Question on list objects
Hi, I have 2 questions on list object:
1. Suppose I have a matrix like:
dat <- matrix(1:9,3)
Now I want to replicate this entire matrix 3 times and put entire result in a list object. Means, if "res" is the resulting list then I should have:
res[[1]]=dat, res[[2]]=dat, res[[3]]=dat
How can I do that in the easilest manner?
2. Suppose I have 2 list objects:
list1 <- list2
2011 Feb 08
2
Extrcat selected rows from a list
Hi,
I have two lists
1) List1- 30,000 rows and 104 columns
2) List2- a list of 14000 selected rownames from List 1
Now, I want to extract all the 104 columns of List1 matching with the 14000 selected rownames from List2.
Psedocode will be something like this:
match rownames(List2) with rownames(List1)
extract selected matched 104 coloumns from (List1)
strore in-> List3
So the
2009 Sep 29
1
Comparing vectors from lists
Hi guys,
I still did not solve my problem properly! I have to compare the values of two lists of 250 numbers as a result of using the ?by function!
List1 of 250
$ 0 : num [1:28] 22 11 31...
$ 1 : num [1:15] 12 14 9 ...
..
..
..
- attr(*, "dim")= int 250
- attr(*, "dimnames")=List of 1
List2 of 250
$ 0 : num [1:24] 20 12 22...
$ 1 : num [1:17] 11 12 19 ...
..
..
2004 Jan 24
1
loop variable passage and lists
I cannot understand why the following expression is accepted (and gives the expected result: to set column 3 and 4 of the first
element of list1 -a data.frame list- as first element of list2):
> list2[[1]]<-list1[[1]][3:4]
...while this one is not (to do the same iteratively from the first to the eleventh element of list1):
> for (i in 1:11){list2[[i]]<-list1[[i]][3:4]}
Error in
2008 Oct 29
1
builtin to filter a list?
I know it's easy to write a simple loop to do this, but in the spirit
of lapply, I thought I would ask if there is a builtin to filter or
take a subset of a list based on a predicate in a similar way to the
Erlang lists:filter/2 function:
http://www.erlang.org/doc/man/lists.html#filter-2
filter(Pred, List1) -> List2
Types:
Pred = fun(Elem) -> bool()
Elem = term()
List1 = List2 =
2010 Jan 28
2
Data frame of different sized lists in a function call
I'm hoping to get some "best practice" feedback for constructing a
function call which takes an undefined set of DIFFERENT length vectors
-- e.g. say we have two lists:
list1=c(1:10)
list2=c(2:4)
lists = data.frame(list1,list2) coerces those two to be the same length
(recycling list2 to fill in the missing rows) -- what is a quick way of
having each of those lists retain their
2012 Jun 11
1
saving sublist lda object with save.image()
Greetings R experts,
I'm having some difficulty recovering lda objects that I've saved within sublists using the save.image() function. I am running a script that exports a variety of different information as a list, included within that list is an lda object. I then take that list and create a list of that with all the different replications I've run. Unfortunately I've been
2002 Oct 14
1
Question: concatenating lists
Dear R users,
I have the following problem. I have a number of lists with identical
structure. Each component is a vector, matrix or array, but components of
different lists may be of different size. How do I combine the lists to get
a new list such that each component of this list contains the components of
the individual lists?
An example may explain most clearly what I need.
Suppose I have
2004 Jun 02
1
How to iterate through two objects of class "list" fast?
Hi!
I have 2 list objects
One list contains the data (list1) the second list2 contains the e.g linear model for each element of list 1.
list2 <- lapply(list1,mylm,response) I obtain list 2 by something like this.
now I have a function myfunc which takes an element from list1 and the coresponding element from list2 does something and ....
list3[[x]] <- myfunc(list1[[x]],list2[[x]])
Until
2009 Jan 19
2
How to assign names in a list
Hi All
How can you associate names with a list when names have not been
assigned? For example if you have a list like this:
list2<-list(1,2,3)
list2
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
How do you make it look like this with names? :
f1<-1
f2<-2
f3<-3
list1<-list(name1=f1, name2=f2, name3=f3)
list1
$name1
[1] 1
$name2
[1] 2
$name3
[1] 3
Thanks for any help.
I expect
2007 Apr 20
2
Fastest way to repeatedly subset a data frame?
Hi -
I have a data frame with a large number of observations (62,000 rows,
but only 2 columns - a character ID and a result list).
Sample:
> my.df <- data.frame(id=c("ID1", "ID2", "ID3"), result=1:3)
> my.df
id result
1 ID1 1
2 ID2 2
3 ID3 3
I have a list of ID vectors. This list will have anywhere from 100 to
1000 members, and
2009 Apr 15
6
Intersection of two sets of intervals
Hi,
Algorithm question: I have two sets of "intervals", where an interval is
an ordered pair [a,b] of two numbers. Is there an efficient way in R to
generate the intersection of two lists of same?
For concreteness: I'm representing a set of intervals with a data.frame:
> list1 = as.data.frame(list(open=c(1,5), close=c(2,10)))
> list1
open close
1 1 2
2 5