[R] selecting the COLUMNS in a dataframe function of the numerical values in a ROW

William Michels wjm1 @end|ng |rom c@@@co|umb|@@edu
Fri Nov 2 02:33:31 CET 2018


Hi Bogdan,

Are you saying you want to drop columns that sum to zero? If so, I'm
not sure you've given us a good example dataframe, since all your
numeric columns give non-zero sums.

Otherwise, what you're asking for is trivial. Below is an example
dataframe ("ygene") with an example "AGA" column that gets dropped:

> xgene <- data.frame(TTT=c(0,1,0,0),
+                TTA=c(0,1,1,0),
+                ATA=c(1,0,0,0),
+                gene=c("gene1", "gene2", "gene3", "gene4"))
>
> xgene[ , colSums(xgene[,1:3]) > 0 ]
  TTT TTA ATA  gene
1   0   0   1 gene1
2   1   1   0 gene2
3   0   1   0 gene3
4   0   0   0 gene4
>
> ygene <- data.frame(TTT=c(0,1,0,0),
+                 TTA=c(0,1,1,0),
+                 AGA=c(0,0,0,0),
+                 gene=c("gene1", "gene2", "gene3", "gene4"))
>
> ygene[ , colSums(ygene[,1:3]) > 0 ]
  TTT TTA  gene
1   0   0 gene1
2   1   1 gene2
3   0   1 gene3
4   0   0 gene4


HTH,

Bill.

William Michels, Ph.D.


On Thu, Nov 1, 2018 at 5:45 PM, Bogdan Tanasa <tanasa using gmail.com> wrote:
> Dear all, please may I ask for a suggestion :
>
> considering a dataframe  that contains the numerical values for gene
> expression, for example :
>
>  x = data.frame(TTT=c(0,1,0,0),
>                TTA=c(0,1,1,0),
>                ATA=c(1,0,0,0),
>                gene=c("gene1", "gene2", "gene3", "gene4"))
>
> how could I select only the COLUMNS where the value of a GENE (a ROW) is
> non-zero ?
>
> thank you !
>
> -- bogdan
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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