[R] Retrieving lists of colnames

Vikash Kumar vikash.kr.117 at gmail.com
Thu Oct 16 12:11:42 CEST 2014


Hi, It works! Only one thing we need to note is when we have only one
column name as an output we do not get the results.


> d=c(11:21)
> e=c(51:61)
> data<-data.frame(embed(1:12,2))
> data=cbind(data,d,e)
> data>6
         X1          X2          d     e
 [1,] FALSE FALSE TRUE TRUE
 [2,] FALSE FALSE TRUE TRUE
 [3,] FALSE FALSE TRUE TRUE
 [4,] FALSE FALSE TRUE TRUE
 [5,] FALSE FALSE TRUE TRUE
 [6,]  TRUE FALSE TRUE TRUE
 [7,]  TRUE  TRUE TRUE TRUE
 [8,]  TRUE  TRUE TRUE TRUE
 [9,]  TRUE  TRUE TRUE TRUE
[10,]  TRUE  TRUE TRUE TRUE
[11,]  TRUE  TRUE TRUE TRUE
> data<-data.frame(data)
*> names(data[,which(apply(data,2,FUN = function(x){all(x>6)}))])*
[1] "d" "e"

Thanks


On Thu, Oct 16, 2014 at 3:26 PM, PIKAL Petr <petr.pikal at precheza.cz> wrote:

> Hi
>
> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> > project.org] On Behalf Of Vikash Kumar
> > Sent: Thursday, October 16, 2014 7:06 AM
> > To: Adam.N.Jenkins13 at gmail.com
> > Cc: r-help at r-project.org
> > Subject: Re: [R] Retrieving lists of colnames
> >
> > Hi Adam,
> >
> > I guess below code would help you achieve the desired ouput.
> >
> > > colnames(data[,which(apply(data,2,FUN = function(x){any(x>0.5)}))])
>
> I do not think so. It will give you overall names but not names for each
> row, which as I understand Adam needs.
>
> Try
>
> > data<-data.frame(embed(1:12,2))
> > data<6
>          X1    X2
>  [1,]  TRUE  TRUE
>  [2,]  TRUE  TRUE
>  [3,]  TRUE  TRUE
>  [4,]  TRUE  TRUE
>  [5,] FALSE  TRUE
>  [6,] FALSE FALSE
>  [7,] FALSE FALSE
>  [8,] FALSE FALSE
>  [9,] FALSE FALSE
> [10,] FALSE FALSE
> [11,] FALSE FALSE
>
> > colnames(data[,which(apply(data,2,FUN = function(x){any(x<6)}))])
> [1] "X1" "X2"
>
>
> > lll<-vector(nrow(data), mode="list")
> > for(i in 1:nrow(data)) lll[[i]]<-colnames(data)[(which(data[i,]<6))]
> > lll
> [[1]]
> [1] "X1" "X2"
>
> [[2]]
> [1] "X1" "X2"
>
> [[3]]
> [1] "X1" "X2"
>
> [[4]]
> [1] "X1" "X2"
>
> [[5]]
> [1] "X2"
>
> [[6]]
> character(0)
>
> [[7]]
> character(0)
>
> [[8]]
> character(0)
>
> [[9]]
> character(0)
>
> [[10]]
> character(0)
>
> [[11]]
> character(0)
>
>
> Cheers
> Petr
>
>
> >
> > Happy Learning!
> >
> > Vikash
> >
> > On Wed, Oct 15, 2014 at 5:53 PM, Adam.N.Jenkins13 at gmail.com <
> > adam.n.jenkins13 at gmail.com> wrote:
> >
> > >
> > >
> > > Hi what I have is a large excel doc (100 columns, 350 row) with data
> > > values from 0-10000. The end goal is for each row to have a list of
> > > colnames of which columns contain values >0. I've been tinkering
> > > around with apply mostly and some other functions, any help offered
> > > would be greatly appreciated.
> > > ______________________________________________
> > > 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.
> > >
> >
> >       [[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.
>
> ________________________________
> Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou
> určeny pouze jeho adresátům.
> Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě
> neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie
> vymažte ze svého systému.
> Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email
> jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
> Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi
> či zpožděním přenosu e-mailu.
>
> V případě, že je tento e-mail součástí obchodního jednání:
> - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření
> smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
> - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout;
> Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany
> příjemce s dodatkem či odchylkou.
> - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve
> výslovným dosažením shody na všech jejích náležitostech.
> - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za
> společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn
> nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto
> emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich
> existence je adresátovi či osobě jím zastoupené známá.
>
> This e-mail and any documents attached to it may be confidential and are
> intended only for its intended recipients.
> If you received this e-mail by mistake, please immediately inform its
> sender. Delete the contents of this e-mail with all attachments and its
> copies from your system.
> If you are not the intended recipient of this e-mail, you are not
> authorized to use, disseminate, copy or disclose this e-mail in any manner.
> The sender of this e-mail shall not be liable for any possible damage
> caused by modifications of the e-mail or by delay with transfer of the
> email.
>
> In case that this e-mail forms part of business dealings:
> - the sender reserves the right to end negotiations about entering into a
> contract in any time, for any reason, and without stating any reasoning.
> - if the e-mail contains an offer, the recipient is entitled to
> immediately accept such offer; The sender of this e-mail (offer) excludes
> any acceptance of the offer on the part of the recipient containing any
> amendment or variation.
> - the sender insists on that the respective contract is concluded only
> upon an express mutual agreement on all its aspects.
> - the sender of this e-mail informs that he/she is not authorized to enter
> into any contracts on behalf of the company except for cases in which
> he/she is expressly authorized to do so in writing, and such authorization
> or power of attorney is submitted to the recipient or the person
> represented by the recipient, or the existence of such authorization is
> known to the recipient of the person represented by the recipient.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list