i wish retain first value of variable group. code below this, uses for-loops
, seems overly complex. there more efficient way, particularly in base r
? object desired.result
contains desired result.
my.data <- read.table(text = ' my.string my.cov my.id 11....... 1 1 1.1...... 3 2 ..1.2.... 4 2 ....2.2.. 5 2 12....... 2 3 .22...... 3 3 ..24..... 3 3 1...2.... 1 4 ....2...4 0 4 ..2..4... 5 5 ', header = true, stringsasfactors = false, na.strings = 'na') desired.result <- read.table(text = ' my.string my.cov my.id 11....... 1 1 1.1...... 3 2 ..1.2.... 3 2 ....2.2.. 3 2 12....... 2 3 .22...... 2 3 ..24..... 2 3 1...2.... 1 4 ....2...4 1 4 ..2..4... 5 5 ', header = true, stringsasfactors = false, na.strings = 'na') my.seq <- rle(my.data$my.id)$lengths my.data$first <- unlist(lapply(my.seq, function(x) seq(1,x))) my.data$last <- unlist(lapply(my.seq, function(x) seq(x,1,-1))) my.data$my.new.cov <- rep(na, nrow(my.data)) for(i in 1:nrow(my.data)) { if(my.data$first[i] == 1) my.data$my.new.cov[i] = my.data$my.cov[i] if(my.data$first[i] > 1) my.data$my.new.cov[i] = my.data$my.new.cov[(i - 1)] } my.data$my.cov <- my.data$my.new.cov my.data <- my.data[, c('my.string', 'my.cov', 'my.id')] all.equal(my.data, desired.result) # [1] true
this seems it:
my.data$my.cov <- ave(my.data$my.cov, my.data$my.id, fun = function(x) head(x,1))