Dimitri Liakhovitski
2009-Sep-17 21:17 UTC
[R] referring to a row number and to a row condition, and to columns simultaneously
Hello, dear R-ers! I have a data frame: x<-data.frame(a=c(4,2,4,1,3,4),b=c(1,3,4,1,5,0),c=c(NA,2,5,3,4,NA),d=rep(NA,6),e=rep(NA,6)) x When x$a==1, I would like to replace NAs in columns d and e with 8 and 9, respectively When x$a != 1, I would like to replace NAs in columns d and e 101 and 1022, respectively. However, I only want to do it for rows 2:5 - while ignoring what's happening in rows 1 and 6. Here is what I've come up with: x for(i in 2:5){ x[i & x[[1]]==1,4:5]<-c(8,9) x[i & x[[1]]!=1,4:5]<-c(101,102) } x However, something is wrong here. First, rows 1 and 6 are not ignored. Second, the order of 101 and 102 changes - I, however, always want to see 101 in column d and 102 in column e. Any advice? Thanks a lot! -- Dimitri Liakhovitski Ninah.com Dimitri.Liakhovitski at ninah.com
Schalk Heunis
2009-Sep-17 21:38 UTC
[R] referring to a row number and to a row condition, and to columns simultaneously
Try this x[(row(x[1]) %in% 2:5) & x$a==1,4:5] <- c(8,9) x[(row(x[1]) %in% 2:5) & x$a!=1,4:5] <- c(101,102) HTH Schalk Heunis On Thu, Sep 17, 2009 at 11:17 PM, Dimitri Liakhovitski <ld7631@gmail.com>wrote:> Hello, dear R-ers! > > I have a data frame: > > x<-data.frame(a=c(4,2,4,1,3,4),b=c(1,3,4,1,5,0),c=c(NA,2,5,3,4,NA),d=rep(NA,6),e=rep(NA,6)) > x > > When x$a==1, I would like to replace NAs in columns d and e with 8 and > 9, respectively > When x$a != 1, I would like to replace NAs in columns d and e 101 and > 1022, respectively. > > However, I only want to do it for rows 2:5 - while ignoring what's > happening in rows 1 and 6. > > Here is what I've come up with: > > x > for(i in 2:5){ > x[i & x[[1]]==1,4:5]<-c(8,9) > x[i & x[[1]]!=1,4:5]<-c(101,102) > } > x > > However, something is wrong here. > First, rows 1 and 6 are not ignored. > Second, the order of 101 and 102 changes - I, however, always want to > see 101 in column d and 102 in column e. > > Any advice? > Thanks a lot! > > -- > Dimitri Liakhovitski > Ninah.com > Dimitri.Liakhovitski@ninah.com > > ______________________________________________ > R-help@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]]
David Winsemius
2009-Sep-17 21:48 UTC
[R] referring to a row number and to a row condition, and to columns simultaneously
On Sep 17, 2009, at 5:17 PM, Dimitri Liakhovitski wrote:> Hello, dear R-ers! > > I have a data frame: > x<-data.frame(a=c(4,2,4,1,3,4),b=c(1,3,4,1,5,0),c=c(NA, > 2,5,3,4,NA),d=rep(NA,6),e=rep(NA,6)) > x > > When x$a==1, I would like to replace NAs in columns d and e with 8 and > 9, respectively > When x$a != 1, I would like to replace NAs in columns d and e 101 and > 1022, respectively. > > However, I only want to do it for rows 2:5 - while ignoring what's > happening in rows 1 and 6. > > Here is what I've come up with: > > x > for(i in 2:5){ > x[i & x[[1]]==1,4:5]<-c(8,9) > x[i & x[[1]]!=1,4:5]<-c(101,102) > } > xx$d[2:5] <- 8*(x$a[2:5] == 1) + 101*(x$a[2:5] != 1) x$e[2:5] <- 9*(x$a[2:5] == 1) + 102*(x$a[2:5] != 1) > x a b c d e 1 4 1 NA NA NA 2 2 3 2 101 102 3 4 4 5 101 102 4 1 1 3 8 9 5 3 5 4 101 102 6 4 0 NA NA NA> > However, something is wrong here. > First, rows 1 and 6 are not ignored. > Second, the order of 101 and 102 changes - I, however, always want to > see 101 in column d and 102 in column e. > > Any advice? > Thanks a lot! > > -- > Dimitri Liakhovitski > Ninah.com > Dimitri.Liakhovitski at ninah.com > > ______________________________________________ > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT
Possibly Parallel Threads
- replacing period with a space
- looping through predictors
- Code is too slow: mean-centering variables in a data frame by subgroup
- More elegant way of excluding rows with equal values in any 2 columns?
- transforming a badly organized data base into a list of data frames