[R] recoding data with loops

Erik Iverson iverson at biostat.wisc.edu
Mon May 19 22:07:58 CEST 2008


Hello -

Donald Braman wrote:
> # I'm new to R and am trying to get the hang of how it handles
> # dataframes & loops. If anyone can help me with some simple tasks,
> # I'd be much obliged.
> 
> # First, i'd like to generate some random data in a dataframe
> # to efficiently illustrate what I'm up to.
> # let's say I have six variables as listed below (I really
> # have hundreds, but a few will illustrate the point).
> # I want to generate my dataframe (mdf)
> # with the 6 variables X 100 values with rnorm(7).
> # How do I do this?  I tried many variations on the following:
> 
> var_list <- c("HEQUAL", "EWEALTH", "ERADEQ", "HREVDIS1", "EDISCRIM",
> "HREVDIS2")
> for(i in 1:length(var_list)) {var_list[1] <- rnorm(100)}
> mdf <- data.frame(cbind(varlist[1:length(var_list)])
> mdf
> 
There are many ways to do this. Do you mean that you want 6 columns, 100 
observations in each column, each a sample from a normal distribution 
with mean = 7 and sd = 1?  You can do this without looping in one of 
several ways.  If you are coming from a SAS environment (my guess since 
you talk of looping over data.frames), you may be used to looping 
through a data object.  In R, you can usually avoid this since many 
functions are vectorized, or take a 'whole object' approach.

var_list <- c("HEQUAL", "EWEALTH", "ERADEQ", "HREVDIS1", "EDISCRIM", 
"HREVDIS2")

mdf <- data.frame(replicate(6, rnorm(100, 7))) ## generate random data
names(mdf) ## default names
names(mdf) <- var_list ## use our names

> 
> # Then, I'd like to recode the variables that begin with the letter "H".
> # I've tried many variations of the following, but to no avail:
> 
> reverse_list <- c("HEQUAL", "HREVDIS1", "HREVDIS2")
> reversed_list <- c("RHEQUAL", "RHREVDIS1", "RHREVDIS2")
> for(i in 1:length(reverse_list))
>  {mdf[ ,e_reversed_list][[i]] <- recode(mdf[ ,e_reverse_list][[i]],
> '5:99=NA; 1=4; 2=3; 3=2; 4=1; ', as.factor.result=FALSE)
> 

I'm not quite sure what you are after here.  What do you mean by recode? 
What package is your 'recode' function located in?

It appears that you may be under the impression that the data.frame 
contains integers, but certainly it will not since it was generated with 
rnorm?  sample can generate a samples of the type you may be after, for 
example,

 > sample(7, 100, replace = TRUE)

Best,
Erik Iverson



More information about the R-help mailing list