Re: [R] recoding data with loops

From: Donald Braman <donald.braman_at_gmail.com>
Date: Mon, 19 May 2008 15:57:47 -0400

Thanks for quick response! I have done. I have tried many configurations of the various examples given there, but the examples are pretty short and none explain how to loop through nonconsecutive variables in a data frame. I've also read dozens of pages that come up when I google "data.frame rnorm" and "data.frame loops", but to no avail.

On Mon, May 19, 2008 at 3:54 PM, Bert Gunter <gunter.berton_at_gene.com> wrote:

> If you're serious, start by reading the docs, especially "An Introduction
> to
> R." There are also other learning resources listed on CRAN.
>
> -- Bert gunter
> Genentech
>
> -----Original Message-----
> From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org]
> On
> Behalf Of Donald Braman
> Sent: Monday, May 19, 2008 12:42 PM
> To: r-help_at_r-project.org
> Subject: [R] recoding data with loops
>
> # 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
>
>
> # 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 sure I have many deep misunderstandings about the R language, but
> # if I can get this much done, I think I'll be well on my way to

> understanding R
> # and how it works with loops and dataframes.

>
> # Thanks!
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
>

-- 
Donald Braman
http://www.law.gwu.edu/Faculty/profile.aspx?id=10123
http://research.yale.edu/culturalcognition
http://ssrn.com/author=286206

	[[alternative HTML version deleted]]

______________________________________________
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 Mon 19 May 2008 - 20:05:16 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 Mon 19 May 2008 - 21:30:46 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