[R] Frequencies for a list of vectors
Dear R users, I have a list of vectors (list is called HTNlist). Each vector is of length 1 to 4 and takes only 0 and 1 as values. E.g. head(HTNlist) $`30008` [1] 1 0 1 0 $`60008` [1] 0 0 1 0 $`90008` [1] 0 0 1 0 $`17` [1] 1 $`130001` [1] 0 1 $`130007` [1] 1 0 1 0 I would like to obtain a frequency table for the elements of the list. I want to know how many of '1 0 0' I have in the list, how many '1 0 1 0' etc. Can you please help? Thank you in advance, MP [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Frequencies for a list of vectors
Hello, Maybe something like table(unlist(lapply(HTNlist, paste, collapse = ''))) (Untested, it's a bad idea not to use ?dput to give a data example.) Use dput(head(HTNlist)) # paste the output of this in a mail Hope this helps, Rui Barradas Em 05-08-2014 18:39, Marie-Pierre Sylvestre escreveu: Dear R users, I have a list of vectors (list is called HTNlist). Each vector is of length 1 to 4 and takes only 0 and 1 as values. E.g. head(HTNlist) $`30008` [1] 1 0 1 0 $`60008` [1] 0 0 1 0 $`90008` [1] 0 0 1 0 $`17` [1] 1 $`130001` [1] 0 1 $`130007` [1] 1 0 1 0 I would like to obtain a frequency table for the elements of the list. I want to know how many of '1 0 0' I have in the list, how many '1 0 1 0' etc. Can you please help? Thank you in advance, MP [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] Frequencies for a list of vectors
You can those vectors into character strings and pass them to table(). E.g., d - list(`30008`=c(1,0,1,0), `60008`=c(0,0,1,0), `90008`=c(0,0,1,0), `17`=1, `130001`=c(0,1), `130007`=c(1,0,1,0)) dChar - vapply(d, FUN=function(di)paste(di, collapse= ), FUN.VALUE=) dTable - table(dChar) dTable dChar 0 0 1 0 0 1 1 1 0 1 0 2 1 1 2 dTable[1 0 1 0] 1 0 1 0 2 Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Aug 5, 2014 at 10:39 AM, Marie-Pierre Sylvestre mp.sylves...@gmail.com wrote: Dear R users, I have a list of vectors (list is called HTNlist). Each vector is of length 1 to 4 and takes only 0 and 1 as values. E.g. head(HTNlist) $`30008` [1] 1 0 1 0 $`60008` [1] 0 0 1 0 $`90008` [1] 0 0 1 0 $`17` [1] 1 $`130001` [1] 0 1 $`130007` [1] 1 0 1 0 I would like to obtain a frequency table for the elements of the list. I want to know how many of '1 0 0' I have in the list, how many '1 0 1 0' etc. Can you please help? Thank you in advance, MP [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] Frequencies for a list of vectors
Alternatively, use sapply instead of lapply marieData - list('30008'=c(1,0,1,0), '60008'=c(0,0,1,0), '90008'=c(0,0,1,0), '17'=1, '130001'=c(0,1)) marieData $`30008` [1] 1 0 1 0 $`60008` [1] 0 0 1 0 $`90008` [1] 0 0 1 0 $`17` [1] 1 $`130001` [1] 0 1 table(sapply(marieData, paste, collapse='')) -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Rui Barradas Sent: Wednesday, 6 August 2014 8:49 a.m. To: Marie-Pierre Sylvestre; r-help@r-project.org Subject: Re: [R] Frequencies for a list of vectors Hello, Maybe something like table(unlist(lapply(HTNlist, paste, collapse = ''))) (Untested, it's a bad idea not to use ?dput to give a data example.) Use dput(head(HTNlist)) # paste the output of this in a mail Hope this helps, Rui Barradas Em 05-08-2014 18:39, Marie-Pierre Sylvestre escreveu: Dear R users, I have a list of vectors (list is called HTNlist). Each vector is of length 1 to 4 and takes only 0 and 1 as values. E.g. head(HTNlist) $`30008` [1] 1 0 1 0 $`60008` [1] 0 0 1 0 $`90008` [1] 0 0 1 0 $`17` [1] 1 $`130001` [1] 0 1 $`130007` [1] 1 0 1 0 I would like to obtain a frequency table for the elements of the list. I want to know how many of '1 0 0' I have in the list, how many '1 0 1 0' etc. Can you please help? Thank you in advance, MP [[alternative HTML version deleted]] __ R-help@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. __ R-help@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. The contents of this e-mail are confidential and may be ...{{dropped:14}} __ R-help@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.
Re: [R] Frequencies for a list of vectors
Using vapply instead of sapply or unlist(lapply) here gives you a little more safety. vapply insists that you supply a FUN.VALUE argument that gives a prototype (type and length) of the expected output of FUN. It will stop if FUN returns something unexpected. Compare the following where I misspelled 'collapse'; only vapply catches the error: marieData - list('30008'=c(1,0,1,0), '60008'=c(0,0,1,0), '90008'=c(0,0,1,0), '17'=1, '130001'=c(0,1)) unlist(lapply(marieData, paste, collaps='')) 300081 300082 300083 300084 600081 600082 600083 600084 900081 900082 1 0 1 0 0 0 1 0 0 0 900083 900084 17 1300011 1300012 1 0 1 0 1 sapply(marieData, paste, collaps='') $`30008` [1] 1 0 1 0 $`60008` [1] 0 0 1 0 $`90008` [1] 0 0 1 0 $`17` [1] 1 $`130001` [1] 0 1 vapply(marieData, paste, collaps='', FUN.VALUE='') Error in vapply(marieData, paste, collaps = , FUN.VALUE = ) : values must be length 1, but FUN(X[[1]]) result is length 4 vapply(X,FUN,FUN.VALUE) also gives you a better result when length(X) is 0, meaning that you don't have to write special code to catch that case. Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Aug 5, 2014 at 1:53 PM, Peter Alspach peter.alsp...@plantandfood.co.nz wrote: Alternatively, use sapply instead of lapply marieData - list('30008'=c(1,0,1,0), '60008'=c(0,0,1,0), '90008'=c(0,0,1,0), '17'=1, '130001'=c(0,1)) marieData $`30008` [1] 1 0 1 0 $`60008` [1] 0 0 1 0 $`90008` [1] 0 0 1 0 $`17` [1] 1 $`130001` [1] 0 1 table(sapply(marieData, paste, collapse='')) -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Rui Barradas Sent: Wednesday, 6 August 2014 8:49 a.m. To: Marie-Pierre Sylvestre; r-help@r-project.org Subject: Re: [R] Frequencies for a list of vectors Hello, Maybe something like table(unlist(lapply(HTNlist, paste, collapse = ''))) (Untested, it's a bad idea not to use ?dput to give a data example.) Use dput(head(HTNlist)) # paste the output of this in a mail Hope this helps, Rui Barradas Em 05-08-2014 18:39, Marie-Pierre Sylvestre escreveu: Dear R users, I have a list of vectors (list is called HTNlist). Each vector is of length 1 to 4 and takes only 0 and 1 as values. E.g. head(HTNlist) $`30008` [1] 1 0 1 0 $`60008` [1] 0 0 1 0 $`90008` [1] 0 0 1 0 $`17` [1] 1 $`130001` [1] 0 1 $`130007` [1] 1 0 1 0 I would like to obtain a frequency table for the elements of the list. I want to know how many of '1 0 0' I have in the list, how many '1 0 1 0' etc. Can you please help? Thank you in advance, MP [[alternative HTML version deleted]] __ R-help@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. __ R-help@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. The contents of this e-mail are confidential and may be ...{{dropped:14}} __ R-help@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. __ R-help@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.
Re: [R] Frequencies for a list of vectors
hi, this may be overkill but i have a general function that tabulates lists of anything. it works on vectors as in your example. the input is a list and the output is a list with 2 elements 1) list of unique values and 2) a matching vector of counts. to that out[[1]][[k]] occurs out[[2]][k] times. a warning: it runs through the list one by one so for long lists it may take a long time cheers, peter table.list - function(my.list) { n.elements - length(my.list) elements.counted - rep(0,n.elements) val.now - my.list[[1]] elements.now - sapply(my.list,function(x) {identical(val.now,x)}) hist.val - list(NULL,val.now) hist.count - c(0,sum(elements.now)) elements.counted - elements.counted + elements.now n.elements.counted - sum(elements.counted) while( n.elements.counted n.elements) { ind.now - min(which(elements.counted == 0)) val.now - my.list[[ind.now]] elements.now - sapply(my.list,function(x) {identical(val.now,x)}) hist.count - c(hist.count,sum(elements.now)) hist.val - lappend(hist.val,val.now) elements.counted - elements.counted + elements.now n.elements.counted - sum(elements.counted) } hist.val[[1]] - NULL hist.count - hist.count[-1] return(list(hist.val,hist.count)) } ## function On Tue, Aug 5, 2014 at 1:39 PM, Marie-Pierre Sylvestre mp.sylves...@gmail.com wrote: Dear R users, I have a list of vectors (list is called HTNlist). Each vector is of length 1 to 4 and takes only 0 and 1 as values. E.g. head(HTNlist) $`30008` [1] 1 0 1 0 $`60008` [1] 0 0 1 0 $`90008` [1] 0 0 1 0 $`17` [1] 1 $`130001` [1] 0 1 $`130007` [1] 1 0 1 0 I would like to obtain a frequency table for the elements of the list. I want to know how many of '1 0 0' I have in the list, how many '1 0 1 0' etc. Can you please help? Thank you in advance, MP [[alternative HTML version deleted]] __ R-help@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. -- Peter Salzman, PhD Department of Biostatistics and Computational Biology University of Rochester [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Frequencies for a list of vectors
it needs a lappend function lappend - function(lst, obj) { lst[[length(lst)+1]] - obj return(lst) } On Tue, Aug 5, 2014 at 10:59 PM, peter salzman peter.salzmanu...@gmail.com wrote: hi, this may be overkill but i have a general function that tabulates lists of anything. it works on vectors as in your example. the input is a list and the output is a list with 2 elements 1) list of unique values and 2) a matching vector of counts. to that out[[1]][[k]] occurs out[[2]][k] times. a warning: it runs through the list one by one so for long lists it may take a long time cheers, peter table.list - function(my.list) { n.elements - length(my.list) elements.counted - rep(0,n.elements) val.now - my.list[[1]] elements.now - sapply(my.list,function(x) {identical(val.now,x)}) hist.val - list(NULL,val.now) hist.count - c(0,sum(elements.now)) elements.counted - elements.counted + elements.now n.elements.counted - sum(elements.counted) while( n.elements.counted n.elements) { ind.now - min(which(elements.counted == 0)) val.now - my.list[[ind.now]] elements.now - sapply(my.list,function(x) {identical(val.now,x)}) hist.count - c(hist.count,sum(elements.now)) hist.val - lappend(hist.val,val.now) elements.counted - elements.counted + elements.now n.elements.counted - sum(elements.counted) } hist.val[[1]] - NULL hist.count - hist.count[-1] return(list(hist.val,hist.count)) } ## function On Tue, Aug 5, 2014 at 1:39 PM, Marie-Pierre Sylvestre mp.sylves...@gmail.com wrote: Dear R users, I have a list of vectors (list is called HTNlist). Each vector is of length 1 to 4 and takes only 0 and 1 as values. E.g. head(HTNlist) $`30008` [1] 1 0 1 0 $`60008` [1] 0 0 1 0 $`90008` [1] 0 0 1 0 $`17` [1] 1 $`130001` [1] 0 1 $`130007` [1] 1 0 1 0 I would like to obtain a frequency table for the elements of the list. I want to know how many of '1 0 0' I have in the list, how many '1 0 1 0' etc. Can you please help? Thank you in advance, MP [[alternative HTML version deleted]] __ R-help@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. -- Peter Salzman, PhD Department of Biostatistics and Computational Biology University of Rochester -- Peter Salzman, PhD Department of Biostatistics and Computational Biology University of Rochester [[alternative HTML version deleted]] __ R-help@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.