The following, locf2(), does what you want using only base R functions:
locf2 <-
function(x, initial=NA, IS_BAD = is.na) {
# Replace 'bad' values in 'x' with last previous non-bad value.
# If no previous non-bad value, replace with 'initial'.
stopifnot(is.function(IS_BAD))
good <-
> On Aug 9, 2017, at 3:39 PM, Ek Esawi wrote:
>
> Thank you so much Jim. I forgot to state that i was hoping to get it
> without loops if possible.
>
> library(zoo)
> is.na(eedf$nam) <- eedf$nam == ""
> eedf$nam <- na.locf(eedf$nam)
> eedf
nam val
1 mandy 1
2 mandy 2
3 John 3
4 Jo
Thank you so much Jim. I forgot to state that i was hoping to get it
without loops if possible.
Thanks again,
EK
[[alternative HTML version deleted]]
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mai
Hi Ek,
As I may have mentioned previously, if you don't mind stepping through
the data frame row by row you can do it like this:
eedf<-read.table(text="nam,val
mandy,1
,2
John,3
,4
,5
,6
Zara,7
,8",sep=",",header=TRUE,stringsAsFactors=FALSE)
for(row in 1:nrow(eedf))
if(eedf$nam[row] == "")
eedf
Hi All—
I was looking at a posting from June-17. I managed to solve it. However,
when I changed the example in the posting, my solution will work only once
at a time which was mentioned by Jim Lemon on his response to the original
posting. This means that my solution will have to be repeated as m
5 matches
Mail list logo