[R] Replace with previous value

From: Ravi S. Shankar <ravis_at_ambaresearch.com>
Date: Wed, 26 Mar 2008 22:48:14 +0530


Hi R,

I have a dataframe with
dim(test)
[1] 435150 4

class(test)
[1] "data.frame"
In the third column every time a number with "-" appears I need to replace with previous value
I am using the following code
s=which(substr(as.character(test[,3]),1,1)=="-")  for(i in 1:length(s)) test[s[i],3] = test[s[i]-1,3]

Is there a faster way of doing this?

sessionInfo()
R version 2.5.1 (2007-06-27)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] "stats" "graphics" "grDevices" "utils" "datasets"
"methods"
[7] "base"

Thanks in advance

Ravi Shankar S  

This e-mail may contain confidential and/or privileged i...{{dropped:10}}



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. Received on Wed 26 Mar 2008 - 17:27:06 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 27 Mar 2008 - 11:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive