« first day (2340 days earlier)      last day (942 days later) » 

5:34 AM
early hello :-)
 
Goodmorning all
 
5:54 AM
Question that seems easy but for some reason I m drawing blanks here :p
Imagine the data frame
df <- data.frame(ID = LETTERS[8:12], v1 = c(0, 0, 'd', 0, 0), v2 = c(0, 0, 0, 0, 'd'), v3 = c('d', 0, 0, 0, 0), v4 = c(0, 0, 0, 'd', 0), stringsAsFactors = FALSE)
What I want to do is simply change all 0 that come AFTER d to NA
 
 
1 hour later…
7:02 AM
f1 <- function(x){
  i1 <- which(x == 'd') + 1
  cond <- length(i1) > 0 && i1 <= length(x)
  if (cond){x[i1:(length(x))] <- NA;x}else{x}
}
df[-1] <- t(apply(df[-1], 1, f1))
My solution
 
hey @Sotos
I'm gonna play with your data, exactly what I needed to sheer me up after a very sh**ty morning...
 
@Cath I hear you! At least it's Friday :D
 
@Sotos not sure the week-end coming will be enough.....
 
7:22 AM
@Cath ohhh okk. So really shitty morning
 
@Sotos library(data.table) ; setDT(df) ; df[, lapply(.SD, function(x) {whD <- which(x=="d") ; x[whD[whD != .N]+1] <- NA_character_ ; x}), .SDcols=-1]
seems to work, you can probably avoid data.table but I never want to ;-)
@Sotos and without internet (currently on my cell phone so I can send you a message)
 
@Cath Hahaha, yeap :p
@Cath ohhh wow!
 
@Sotos hmm morning began by: we (my neighbours and me) tried to catch a f***ing roaming cat so it can go to a clinic and after that to a specialized organisation, we couldn't, we almost got hurt and, in the middle of that, I broke my phone (the screen) because one of my neighbours wanted to know what time it was....
that's a part of the sh** only
I feel this week is not mine :-/
is the sol ok for you ?
 
@Cath Its ok. It only means that the next one will only be better
@Cath No, it doesnt give the expected output
 
@Sotos it did with your data, strange
 
7:34 AM
v1 v2   v3   v4
1:    0  0    d    0
2:    0  0 <NA>    0
3:    d  0    0    0
4: <NA>  0    0    d
5:    0  d    0 <NA>
This is what I get
 
or I didn't understant what you needed
yep, 0 after a d change to NA
 
I need this:
 
(so 2nd option : I didn't understand what your need was ^^)
 
ID v1   v2   v3   v4
1  H  0    0    d <NA>
2  I  0    0    0    0
3  J  d <NA> <NA> <NA>
4  K  0    0    0    d
5  L  0    d <NA> <NA>
 
oh ok, absolutely not the same, it's "row-after" ! sorry !! (I should have passed your code to see ^^)
hmm, I'm sure I saw stuff like that on SO
 
7:36 AM
Me too
But I couldnt find it
 
:-(
ok, I'll try again ^^
 
Ok Cath. Thanks.
A data.table solution is always neede :p
 
the "byrow" is never very efficient but probably you can't do without
@Sotos :-)
 
I tried casting to long
But got stack
 
how big is your real df ?
 
7:41 AM
Not very big
around 8 cols and like 150K rows
 
hmm, lots of rows...
 
relatively :p
 
8:03 AM
hello :-)
 
8:40 AM
@Sotos : nit very satisfied with do.call(rbind, lapply(1:nrow(df), function(i) {x <- unlist(df[i, .SD, .SDcols=-1]) ; whD <- min(which(x=="d")) ; if(is.finite(whD) & whD != length(x)) {x[(whD[whD != length(x)]+1):length(x)] <- NA_character_ }; x})) but not able to do better for now :-/
(plus, it returns a matrix which is absolutely not a good idea...)
 
@Cath I know! I was also baffled. I thought it was much easier than this
@Cath Thanks for it tho :)))
 
you're welcome but it's ugly, sorry :-(
 
@Cath No problem at all :D
 
@Sotos another solution:
w <- which(df == "d", arr.ind = TRUE)
w <- w[w[,2] < ncol(df),]
reps <- ncol(df) - w[,2]
w <- w[rep(1:nrow(w), reps),]
w[,2] <- w[,2] + unlist(sapply(reps, seq))

df[w] <- NA
 
8:56 AM
@Jaap Ohhhhh smart! Thanks Jaap
I think effiencywise will be much better
 
9:14 AM
@Sotos yw :-)
@Sotos 1.5 seconds on 500k rows & about 3 minutes on 50 million
 
@Jaap That ll do :DD
 
 
11 hours later…
8:33 PM
7
Q: Planned SEDE maintenance scheduled for Sept 22+24, 2021 at 01:00 to 04:00 UTC (Sept 21+23 9:00 PM to Midnight US/Eastern)

Jade SpragueDue to a planned network upgrade, data.stackexchange.com (a.k.a. SEDE or the Stack Exchange Data Explorer) may be unavailable for up to 6 hours this week, likely considerably less. The first window will be Tuesday, Sept 21, 2021 between 9pm and midnight US/Eastern (a.k.a. NYC-time). The second ...

 

« first day (2340 days earlier)      last day (942 days later) »