David Kane
2004-Sep-15 18:44 UTC
[R] replacing NA's with 0 in a dataframe for specified columns
I know that there must be a cool way of doing this, but I can't think of it. Let's say I have an dataframe with NA's.> x <- data.frame(a = c(0,1,2,NA), b = c(0,NA,1,2), c = c(NA, 0, 1, 2)) > xa b c 1 0 0 NA 2 1 NA 0 3 2 1 1 4 NA 2 2>I know it is easy to replace all the NA's with zeroes.> x[is.na(x)] <- 0 > xa b c 1 0 0 0 2 1 0 0 3 2 1 1 4 0 2 2>But how do I do this for just columns a and c, leaving the NA in column b alone? Thanks, Dave Kane> R.version_ platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 1 minor 9.1 year 2004 month 06 day 21 language R>
Spencer Graves
2004-Sep-15 19:01 UTC
[R] replacing NA's with 0 in a dataframe for specified columns
Have you considered the following: > x <- data.frame(a = c(0,1,2,NA), b = c(0,NA,1,2), c = c(NA, 0, 1, 2)) > x$a[is.na(x$a)] <- 0 > x$c[is.na(x$c)] <- 0 > x a b c 1 0 0 0 2 1 NA 0 3 2 1 1 4 0 2 2 hope this helps. spencer graves David Kane wrote:>I know that there must be a cool way of doing this, but I can't think >of it. Let's say I have an dataframe with NA's. > > > >>x <- data.frame(a = c(0,1,2,NA), b = c(0,NA,1,2), c = c(NA, 0, 1, 2)) >>x >> >> > a b c >1 0 0 NA >2 1 NA 0 >3 2 1 1 >4 NA 2 2 > > > >I know it is easy to replace all the NA's with zeroes. > > > >>x[is.na(x)] <- 0 >>x >> >> > a b c >1 0 0 0 >2 1 0 0 >3 2 1 1 >4 0 2 2 > > > >But how do I do this for just columns a and c, leaving the NA in >column b alone? > >Thanks, > >Dave Kane > > > >>R.version >> >> > _ >platform i686-pc-linux-gnu >arch i686 >os linux-gnu >system i686, linux-gnu >status >major 1 >minor 9.1 >year 2004 >month 06 >day 21 >language R > > > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >-- Spencer Graves, PhD, Senior Development Engineer O: (408)938-4420; mobile: (408)655-4567
Corey Moffet
2004-Sep-15 19:05 UTC
[R] replacing NA's with 0 in a dataframe for specified columns
try: x[is.na(x$a) | is.na(x$c),] <- 0 At 02:44 PM 9/15/2004 -0400, David Kane wrote:>I know that there must be a cool way of doing this, but I can't think >of it. Let's say I have an dataframe with NA's. > >> x <- data.frame(a = c(0,1,2,NA), b = c(0,NA,1,2), c = c(NA, 0, 1, 2)) >> x > a b c >1 0 0 NA >2 1 NA 0 >3 2 1 1 >4 NA 2 2 >> > >I know it is easy to replace all the NA's with zeroes. > >> x[is.na(x)] <- 0 >> x > a b c >1 0 0 0 >2 1 0 0 >3 2 1 1 >4 0 2 2 >> > >But how do I do this for just columns a and c, leaving the NA in >column b alone? > >Thanks, > >Dave Kane > >> R.version > _ >platform i686-pc-linux-gnu >arch i686 >os linux-gnu >system i686, linux-gnu >status >major 1 >minor 9.1 >year 2004 >month 06 >day 21 >language R >> > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >With best wishes and kind regards I am Sincerely, Corey A. Moffet Rangeland Scientist ################################################################## #### USDA-ARS # Northwest Watershed Research Center # 800 Park Blvd, Plaza IV, Suite 105 ########### #### Boise, ID 83712-7716 # # # # Voice: (208) 422-0718 # # #### #### FAX: (208) 334-1502 # # # # #### ########### ##################################################################
John Fox
2004-Sep-15 19:12 UTC
[R] replacing NA's with 0 in a dataframe for specified columns
Dear David, How about the following? cols <- c(1,3) x[,cols][is.na(x[,cols])] <- 0 I hope that this helps, John On Wed, 15 Sep 2004 14:44:53 -0400 David Kane <dave at kanecap.com> wrote:> I know that there must be a cool way of doing this, but I can't think > of it. Let's say I have an dataframe with NA's. > > > x <- data.frame(a = c(0,1,2,NA), b = c(0,NA,1,2), c = c(NA, 0, 1, > 2)) > > x > a b c > 1 0 0 NA > 2 1 NA 0 > 3 2 1 1 > 4 NA 2 2 > > > > I know it is easy to replace all the NA's with zeroes. > > > x[is.na(x)] <- 0 > > x > a b c > 1 0 0 0 > 2 1 0 0 > 3 2 1 1 > 4 0 2 2 > > > > But how do I do this for just columns a and c, leaving the NA in > column b alone? > > Thanks, > > Dave Kane > > > R.version > _ > platform i686-pc-linux-gnu > arch i686 > os linux-gnu > system i686, linux-gnu > status > major 1 > minor 9.1 > year 2004 > month 06 > day 21 > language R > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html-------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/
Chuck Cleland
2004-Sep-15 19:12 UTC
[R] replacing NA's with 0 in a dataframe for specified columns
mydata <- data.frame(a = c(0,1,2,NA), b = c(0,NA,1,2), c = c(NA, 0, 1, 2)) mydata a b c 1 0 0 NA 2 1 NA 0 3 2 1 1 4 NA 2 2 mydata[,c("a", "c")] <- apply(mydata[,c("a","c")], 2, function(x){replace(x, is.na(x), 0)}) mydata a b c 1 0 0 0 2 1 NA 0 3 2 1 1 4 0 2 2 David Kane wrote:> I know that there must be a cool way of doing this, but I can't think > of it. Let's say I have an dataframe with NA's. > > >>x <- data.frame(a = c(0,1,2,NA), b = c(0,NA,1,2), c = c(NA, 0, 1, 2)) >>x > > a b c > 1 0 0 NA > 2 1 NA 0 > 3 2 1 1 > 4 NA 2 2 > > > I know it is easy to replace all the NA's with zeroes. > > >>x[is.na(x)] <- 0 >>x > > a b c > 1 0 0 0 > 2 1 0 0 > 3 2 1 1 > 4 0 2 2 > > > But how do I do this for just columns a and c, leaving the NA in > column b alone? > > Thanks, > > Dave Kane > > >>R.version > > _ > platform i686-pc-linux-gnu > arch i686 > os linux-gnu > system i686, linux-gnu > status > major 1 > minor 9.1 > year 2004 > month 06 > day 21 > language R > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894
Gabor Grothendieck
2004-Sep-15 20:20 UTC
[R] replacing NA's with 0 in a dataframe for specified columns
The col funtion can be helpful here. We want to satisfy two conditions: 1. the element is an NA 2. the element lies in one of the specified columns The first two lines below calculate logical vectors for these two, respectively, and the last line assigns 0 to those elements. isna <- is.na(x) iscol <- col(isna) %in% c(1,3) x[isna & iscol] <- 0 To specify the columns by name rather than number first calculate their column numbers and then proceed as before. That is, replace the second line by: cols <- match(c("a", "c"), colnames(x)) iscol <- col(isna) %in% cols David Kane <dave <at> kanecap.com> writes: : : I know that there must be a cool way of doing this, but I can't think : of it. Let's say I have an dataframe with NA's. : : > x <- data.frame(a = c(0,1,2,NA), b = c(0,NA,1,2), c = c(NA, 0, 1, 2)) : > x : a b c : 1 0 0 NA : 2 1 NA 0 : 3 2 1 1 : 4 NA 2 2 : > : : I know it is easy to replace all the NA's with zeroes. : : > x[is.na(x)] <- 0 : > x : a b c : 1 0 0 0 : 2 1 0 0 : 3 2 1 1 : 4 0 2 2 : > : : But how do I do this for just columns a and c, leaving the NA in : column b alone? : : Thanks, : : Dave Kane : : > R.version : _ : platform i686-pc-linux-gnu : arch i686 : os linux-gnu : system i686, linux-gnu : status : major 1 : minor 9.1 : year 2004 : month 06 : day 21 : language R : > : : ______________________________________________ : R-help <at> stat.math.ethz.ch mailing list : https://stat.ethz.ch/mailman/listinfo/r-help : PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html : :