Displaying 20 results from an estimated 60000 matches similar to: "Subsetting Lists"
2008 Mar 12
2
subset list based on logical within element flag
I have a very long list that I'd like to subset based on a logical value
within each element. Example below. I'd like to get just those list
elements for further study whose $sig.cor slot is TRUE. In this example,
I'd only want element [[2]].
Should be simple, I know. How can I do this? Thanks, Mark
> gene.pair.tf.lst
[[1]]
[[1]]$gene.pair
[1] "Lgals1:Pxmp2"
2010 Jul 07
2
Trimming in R
I am looking for a way to trim leading and trailing spaces in a character
string in R. For example:
" this is random text "
should become:
"this is random text".
I have a short function to perform this task as follows:
trim <- function(str){
str <- sub("^ +", "", str)
str <- sub(" +$", "", str)
}
2008 Sep 03
3
subsetting a data frame
I have a data frame that looks like this:
V1 V2 V3
a b 0:1:12
d f 1:2:1
c d 1:0:9
where V3 is in the form x:y:z
Can someone show me how to subset the rows where the values of x, y and z <= 10:
V1 V2 V3
d f 1:2:1
c d 1:0:9
Thanks
Joseph
[[alternative HTML version deleted]]
2010 Jun 23
4
list operation
Hi,
it seems a simple problem, but I can not find a clear way.
I have a list:
lst=list(m=c('a','b','c'),n=c('c','a'),l=c('a','bc'))
> lst
$m
[1] "a" "b" "c"
$n
[1] "c" "a"
$l
[1] "a" "bc"
how can I get list elements that include a given subset? for example, for given
2013 Jan 11
3
Access comonents in lists of lists
Dear R users,I have a list of equally structured lists, how can I access e.g.
all 2nd compontents in those sub-lists?An example:lst <-
list(rep(list(1:3),3), rep(list(4:6),3))> lst[[1]][[1]][[1]][1] 1 2
3[[1]][[2]][1] 1 2 3[[1]][[3]][1] 1 2 3[[2]][[2]][[1]][1] 4 5 6[[2]][[2]][1]
4 5 6[[2]][[3]][1] 4 5 6What I want to get are all second sub-lists, in this
case:[[1]][[2]][1] 1 2
2007 Jul 14
3
Extracting elements from a list
Hi,
I would love an easy way to extract elements from a list.
For example, if I want the first element from each of 10 arrays stored
in a list,
Lst[[1:10]][1,1] seems like a logical approach, but gives this error:
"Error: recursive indexing failed at level 3"
The following workaround is functional but can get annoying/confusing.
first.element=vector()
for (i in 1:10){
2011 May 17
4
subsetting a list of dataframes
Hello All,
I have a list of dataframes, and I need to subset it by keeping only those
dataframes in the list that meet a certain criterion. Specifically, I need
to generate a second list which only includes those dataframes whose number
of rows is > 1.
Could someone suggest how to do this? I have come close to what I need with
loops and such, but there must be a less clumsy way...
Many
2012 Nov 26
3
Passing lists between functions
I'd like to pass a list object created by one function as an argument of
another function. once inside the second function, I'd like to break the
list up to it's individual elements, each then identifiable by the 'names'
of the list.
The list looks something like
lst<-list(a=1, b=2, df=5, g=7)
then inside the function I've been writing a sequence of statements that
2008 Oct 07
1
using assign with lists
I am performing many permutations on a data-set with each permutation
producing a variable number of results. I thought that the best way to keep
track of all this in one object would be with a list ('res.lst'). To address
these variable results for each permutation I attempted to construct this
list using 'assign'. There is even more nesting than indicated below, but
this is a
2007 Apr 05
2
creating a data frame from a list
Dear all,
A few months ago, I asked for your help on the following problem:
I have a list with three (named) numeric vectors:
> lst = list(a=c(A=1,B=8) , b=c(A=2,B=3,C=0), c=c(B=2,D=0) )
> lst
$a
A B
1 8
$b
A B C
2 3 0
$c
B D
2 0
Now, I'd love to use this list to create the following data frame:
> dtf = data.frame(a=c(A=1,B=8,C=NA,D=NA),
+
2008 Jul 01
1
extracting elements from a list in vectorized form
Hi;
It seems to me that has probably been asked in the past. But I cannot find
the track.
I usually need to extract elements from a list and contruct vector from
them; e.g., to create a table. Perhaps there is a way to directly extract
them without looping?
Simple example:
> S.lst
$sublist.1
$sublist.1$age
[1] 24.58719
$sublist.1$weight
[1] 60.82861
$sublist.2
$sublist.2$age
[1] 32.39551
2013 Jul 02
2
cache most-recent dispatch
Hi,
S4 method dispatch can be very slow. Would it be reasonable to cache the
most
recent dispatch, anticipating the next invocation will be on the same
type? This
would be very helpful in loops.
fun0 <- function(x)
sapply(x, paste, collapse="+")
fun1 <- function(x) {
paste <- selectMethod(paste, class(x[[1]]))
sapply(x, paste,
2010 Jun 14
2
list matching
Hello,
I could not find a clear solution for the follow question. please allow me to ask. thanks
mynames=cbind(c('a','b'),c(11,22))
lst=list(a=c(1,2), b=5)
now I try to combine mynames and lst:
a 1 11
a 2 11
b 5 22
thanks
jian
[[alternative HTML version deleted]]
2009 Jul 23
3
How to perform a calculation in each element of my list?
Hi R-helpers,
I have a list containing 10 elements, each of which is a dataframe. I wish
to add a new column to each list element (dataframe) containing the product
of the last two columns of each dataframe.
I'd appreciate any pointers, thanks!
Mark Na
[[alternative HTML version deleted]]
2008 Jun 04
1
Similar question about subsetting data
This is just a slight modification of a question I asked earlier. Thanks to all the responders.
I have a data set(Bill) of with 1 variable (var1), with 100 obs that are in ascending order. I want to sample every 10 observations and save them in 10 different groups such as Group1 is obs 1-10 Group 2 is obs-11-20 .... Subset data into the 10 groups, then calculate the mean/0.8 of var1 for each
2012 Mar 11
2
Efficient access to elements of a list of lists
Hi,
I have a long list of lists from which I want to efficiently extract
and rbind elements. So I'm using the approach below:
f <- function(i){
out <- replicate(5, list(matrix(rnorm(80), nc=20)))
names(out) <- letters[1:5]
out
}
set.seed(1)
lst <- lapply(1:1.5e6, f)
(t0 <- system.time(tmp <- do.call(rbind, lapply(lst, '[[', 'b'))))
Is there
2010 Dec 07
5
fast subsetting of lists in lists
Hello,
my data is contained in nested lists (which seems not necessarily to be
the best approach). What I need is a fast way to get subsets from the data.
An example:
test <- list(list(a = 1, b = 2, c = 3), list(a = 4, b = 5, c = 6),
list(a = 7, b = 8, c = 9))
Now I would like to have all values in the named variables "a", that is
the vector c(1, 4, 7). The best I could come up
2009 Jun 15
1
Create R object
Dear R users,
I have two simple questions here, and hope someone can help me on this. Thanks in advance.
1.
I have a list object lst=list(a1=matrix(rnorm(4),2,2), a2=matrix(rnorm(4),2,2),a3=matrix(rnorm(4),2,2)). Here I only use three elements for illustration, and in fact the length of lst, n, is unknown in advance. I want to define an object for each element of this lst, and the objects have
2013 Apr 18
5
Subsetting a large number into smaller numbers and find the largest product
Hello,
I have a big number lets say of around hundred digits. I want to subset
that big number into consecutive number of 5 digits and find the product of
those 5 digits. For example my first 5 digit number would be 73167. I need
to check the product of the individual numbers in 73167 and so on.
The sample number is as follows:
2012 Nov 24
5
subsetting - questions
Hello,
I have two very basic questions (console attached):
1) What am I getting an error message for # 5 and # 7 ?
2) How to fix the code?
I would appreciate receiving your help.
Thanks,
Pradip Muhuri
###### Reproducible Example #####
N <- 100
set.seed(13)
df<-data.frame(matrix(sample(c(1:10),N, replace=TRUE),ncol=5))
keep_var <- c("X1", "X2")
drop_var