hi i have a dataframe "a" which looks like: column1, column2, column3 10,12, 0 NA, 0,1 12,NA,50 i want to replace all values in column1 to column3 which do not contain "NA" with values of vector "b" (100,200,300). any idea i can do it? i appreciate any hint regards lukas ??? Lukas Indermaur, PhD student eawag / Swiss Federal Institute of Aquatic Science and Technology ECO - Department of Aquatic Ecology ?berlandstrasse 133 CH-8600 D?bendorf Switzerland Phone: +41 (0) 71 220 38 25 Fax : +41 (0) 44 823 53 15 Email: lukas.indermaur at eawag.ch www.lukasindermaur.ch
On Wed, 2007-01-24 at 20:27 +0100, Indermaur Lukas wrote:> hi > i have a dataframe "a" which looks like: > > column1, column2, column3 > 10,12, 0 > NA, 0,1 > 12,NA,50 > > i want to replace all values in column1 to column3 which do not contain "NA" with values of vector "b" (100,200,300). > > any idea i can do it? > > i appreciate any hint > regards > lukas >Here is one possibility:> sapply(seq(along = colnames(DF)),function(x) ifelse(is.na(DF[[x]]), 100 * x, DF[[x]])) [,1] [,2] [,3] [1,] 10 12 0 [2,] 100 0 1 [3,] 12 200 50 Note that the returned object will be a matrix, so if you need a data frame, just coerce the result with as.data.frame(). HTH, Marc Schwartz
Hint: Try ?subset at the R prompt "Indermaur Lukas" <Lukas.Indermaur at eawag.ch> wrote:> hi > i have a dataframe "a" which looks like: > > column1, column2, column3 > 10,12, 0 > NA, 0,1 > 12,NA,50 > > i want to replace all values in column1 to column3 which do not contain "NA" with values of vector "b" (100,200,300). > > any idea i can do it? > > i appreciate any hint-- Mike Prager, NOAA, Beaufort, NC * Opinions expressed are personal and not represented otherwise. * Any use of tradenames does not constitute a NOAA endorsement.