conditional replace column values with values from a different column
1
0
Entering edit mode
Assa Yeroslaviz ★ 1.5k
@assa-yeroslaviz-1597
Last seen 3 months ago
Germany
Hi R Users, I have a data.frame with various NAs (df name is 'bp_subset'): GOBPIDPvalueTermPvalue2Term2 1GO:00000414.991241e-03transition metal ion transport0.0305047593transition metal ion transport 2GO:0000103NANA 0.0475575125sulfate assimilation 3GO:0000904NANA0.0211635899cell morphogenesis involved in differentiation 4GO:0001505NANA0.0314384424regulation of neurotransmitter levels 5GO:0001508NANA0.0017933627regulation of action potential 6GO:00016594.018995e-02temperature homeostasisNANA I would like tobe abale to replace the 'NA' values of the column "Term", with the values of column "Term2" (if they are not 'NA') I tried to do it with an if statement, but it doesn't work correctly. > if is.na(bp_subset[,3])) bp_subset[,3] <- bp_subset[,5]Warning message:In if is.na(bp_subset[, 3])) bp_subset[, 3] <- bp_subset[, 5] : the condition has length > 1 and only the first element will be used I also tried to do something like: bp_subset_test[is.na(bp_subset_test$Term)] <- bp_subset_test$Term2 Error in `[<-.data.frame`(`*tmp*`, is.na(bp_subset_test$Term), value = c("transition metal ion transport", : duplicate subscripts for columns Is there another way of doing it? THX, Assa > sessionInfo()R version 2.12.2 (2011-02-25) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] C attached base packages: [1] splines grid stats graphics grDevices utils datasets methods base other attached packages: [1] GSEABase_1.12.1 org.Mm.eg.db_2.4.6 Heatplus_1.20.0 ggplot2_0.8.9 proto_0.3-9.1 [6] reshape_0.8.4 plyr_1.4 gplots_2.8.0 caTools_1.11 bitops_1.0-4.1 [11] gdata_2.8.1 gtools_2.6.2 siggenes_1.24.0 multtest_2.7.1 Rgraphviz_1.29.0 [16] xtable_1.5-6 annotate_1.28.1 GO.db_2.4.5 biomaRt_2.6.0 GOstats_2.16.0 [21] RSQLite_0.9-4 DBI_0.2-5 graph_1.28.0 Category_2.16.0 AnnotationDbi_1.12.0 [26] Biobase_2.10.0 loaded via a namespace (and not attached): [1] MASS_7.3-11 RBGL_1.26.0 RCurl_1.5-0 XML_3.2-0 genefilter_1.32.0 survival_2.36-5 [7] tools_2.12.2 [[alternative HTML version deleted]]
GO GO • 8.3k views
ADD COMMENT
0
Entering edit mode
@wolfgang-huber-3550
Last seen 14 days ago
EMBL European Molecular Biology Laborat…
Hi Assa bp_subset$Term = with(bp_subset, ifelseis.na(Term), Term2, Term)) The 'with' is tangential and could be omitted if you prefer to spell out the 'bp_subset$' three times. Wolfgang Il Apr/8/11 9:48 AM, Assa Yeroslaviz ha scritto: > Hi R Users, > > I have a data.frame with various NAs (df name is 'bp_subset'): > GOBPIDPvalueTermPvalue2Term2 1GO:00000414.991241e-03transition metal ion > transport0.0305047593transition metal ion transport 2GO:0000103NANA > 0.0475575125sulfate assimilation 3GO:0000904NANA0.0211635899cell > morphogenesis involved in differentiation > 4GO:0001505NANA0.0314384424regulation > of neurotransmitter levels 5GO:0001508NANA0.0017933627regulation of action > potential 6GO:00016594.018995e-02temperature homeostasisNANA > I would like tobe abale to replace the 'NA' values of the column "Term", > with the values of column "Term2" (if they are not 'NA') > I tried to do it with an if statement, but it doesn't work correctly. > >> if is.na(bp_subset[,3])) > bp_subset[,3]<- bp_subset[,5]Warning message:In if > is.na(bp_subset[, 3])) bp_subset[, 3]<- bp_subset[, 5] : > the condition has length> 1 and only the first element will be used > > I also tried to do something like: > > bp_subset_test[is.na(bp_subset_test$Term)]<- bp_subset_test$Term2 > Error in `[<-.data.frame`(`*tmp*`, is.na(bp_subset_test$Term), value = > c("transition metal ion transport", : > duplicate subscripts for columns > > > Is there another way of doing it? > > THX, > > Assa > > >> sessionInfo()R version 2.12.2 (2011-02-25) > Platform: x86_64-pc-linux-gnu (64-bit) > > locale: > [1] C > > attached base packages: > [1] splines grid stats graphics grDevices utils > datasets methods base > > other attached packages: > [1] GSEABase_1.12.1 org.Mm.eg.db_2.4.6 Heatplus_1.20.0 > ggplot2_0.8.9 proto_0.3-9.1 > [6] reshape_0.8.4 plyr_1.4 gplots_2.8.0 > caTools_1.11 bitops_1.0-4.1 > [11] gdata_2.8.1 gtools_2.6.2 siggenes_1.24.0 > multtest_2.7.1 Rgraphviz_1.29.0 > [16] xtable_1.5-6 annotate_1.28.1 GO.db_2.4.5 > biomaRt_2.6.0 GOstats_2.16.0 > [21] RSQLite_0.9-4 DBI_0.2-5 graph_1.28.0 > Category_2.16.0 AnnotationDbi_1.12.0 > [26] Biobase_2.10.0 > > loaded via a namespace (and not attached): > [1] MASS_7.3-11 RBGL_1.26.0 RCurl_1.5-0 XML_3.2-0 > genefilter_1.32.0 survival_2.36-5 > [7] tools_2.12.2 > > [[alternative HTML version deleted]] > > _______________________________________________ > Bioconductor mailing list > Bioconductor at r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor -- Wolfgang Huber EMBL http://www.embl.de/research/units/genome_biology/huber
ADD COMMENT
0
Entering edit mode
Hi Assa, Wolfgang, On 11-04-08 01:06 AM, Wolfgang Huber wrote: > Hi Assa > > bp_subset$Term = with(bp_subset, ifelseis.na(Term), Term2, Term)) > > The 'with' is tangential and could be omitted if you prefer to spell out > the 'bp_subset$' three times. Or slightly faster: idx <- whichis.na(bp_subset$Term)) bp_subset$Term[idx] <- bp_subset$Term2[idx] H. > > Wolfgang > > Il Apr/8/11 9:48 AM, Assa Yeroslaviz ha scritto: >> Hi R Users, >> >> I have a data.frame with various NAs (df name is 'bp_subset'): >> GOBPIDPvalueTermPvalue2Term2 1GO:00000414.991241e-03transition metal ion >> transport0.0305047593transition metal ion transport 2GO:0000103NANA >> 0.0475575125sulfate assimilation 3GO:0000904NANA0.0211635899cell >> morphogenesis involved in differentiation >> 4GO:0001505NANA0.0314384424regulation >> of neurotransmitter levels 5GO:0001508NANA0.0017933627regulation of >> action >> potential 6GO:00016594.018995e-02temperature homeostasisNANA >> I would like tobe abale to replace the 'NA' values of the column "Term", >> with the values of column "Term2" (if they are not 'NA') >> I tried to do it with an if statement, but it doesn't work correctly. >> >>> if is.na(bp_subset[,3])) >> bp_subset[,3]<- bp_subset[,5]Warning message:In if >> is.na(bp_subset[, 3])) bp_subset[, 3]<- bp_subset[, 5] : >> the condition has length> 1 and only the first element will be used >> >> I also tried to do something like: >> >> bp_subset_test[is.na(bp_subset_test$Term)]<- bp_subset_test$Term2 >> Error in `[<-.data.frame`(`*tmp*`, is.na(bp_subset_test$Term), value = >> c("transition metal ion transport", : >> duplicate subscripts for columns >> >> >> Is there another way of doing it? >> >> THX, >> >> Assa >> >> >>> sessionInfo()R version 2.12.2 (2011-02-25) >> Platform: x86_64-pc-linux-gnu (64-bit) >> >> locale: >> [1] C >> >> attached base packages: >> [1] splines grid stats graphics grDevices utils >> datasets methods base >> >> other attached packages: >> [1] GSEABase_1.12.1 org.Mm.eg.db_2.4.6 Heatplus_1.20.0 >> ggplot2_0.8.9 proto_0.3-9.1 >> [6] reshape_0.8.4 plyr_1.4 gplots_2.8.0 >> caTools_1.11 bitops_1.0-4.1 >> [11] gdata_2.8.1 gtools_2.6.2 siggenes_1.24.0 >> multtest_2.7.1 Rgraphviz_1.29.0 >> [16] xtable_1.5-6 annotate_1.28.1 GO.db_2.4.5 >> biomaRt_2.6.0 GOstats_2.16.0 >> [21] RSQLite_0.9-4 DBI_0.2-5 graph_1.28.0 >> Category_2.16.0 AnnotationDbi_1.12.0 >> [26] Biobase_2.10.0 >> >> loaded via a namespace (and not attached): >> [1] MASS_7.3-11 RBGL_1.26.0 RCurl_1.5-0 XML_3.2-0 >> genefilter_1.32.0 survival_2.36-5 >> [7] tools_2.12.2 >> >> [[alternative HTML version deleted]] >> >> _______________________________________________ >> Bioconductor mailing list >> Bioconductor at r-project.org >> https://stat.ethz.ch/mailman/listinfo/bioconductor >> Search the archives: >> http://news.gmane.org/gmane.science.biology.informatics.conductor > -- Hervé Pagès Program in Computational Biology Division of Public Health Sciences Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N, M2-B876 P.O. Box 19024 Seattle, WA 98109-1024 E-mail: hpages at fhcrc.org Phone: (206) 667-5791 Fax: (206) 667-1319
ADD REPLY

Login before adding your answer.

Traffic: 827 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6