[R] Split a list

R. Michael Weylandt michael.weylandt at gmail.com
Fri Oct 14 15:56:34 CEST 2011


Comments inline:

On Fri, Oct 14, 2011 at 9:06 AM, Juliet Ndukum <jpntsang at yahoo.com> wrote:
> I have a list of dataframes i.e. each list element is a dataframe with three columns and differing number of rows. The third column takes on only two values. I wish to split the list into two sublists based on the value of the third column of the list element.

Assuming the third column always takes a constant value for each
data.frame, something this will work:

lst <- list(a = data.frame(1:3, 4:6, 7), b = data.frame(1:3, 4:6, 8),
c = data.frame(1:3, 4:6, 7))
idx <- sapply(lst, function(x) x[1,3] == 7)

lst1 = lst[idx]
lst2 = lst[!idx]


> Second issue with lists as well. I would like to reduce each of the sublist based on the range of the second column, i.e. if the range of the second column is greater than twenty for example keep the list element.

Similar line of argument:

idx <- sapply(lst, function(x) range(x[,2]) > 20)
lst1 = lst[idx]

>
> Could someone help me with a code to implement these two issues. Thanks in advance for your help,
> JN
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list