-1

This is a rather straightforward thing in other languages and most likely in R too, but I am new to R so help is greatly appreciated.

I have a 100 variables with a suffix 1, 2,...,100, and would like to run the same command for each and one of them.

I would like to use some replacement variable or some for loop in order to loop and run the same command for the following ("numeric") variables:

ln_income1
ln_income2
ln_income3
...
ln_income99
ln_income100

Let's just call the replacement variable {replace} (even if I know that I cannot call it this), and loop this over 1, 2,...,100 in:

ln_income{replace}.haar <- mra(ln_income{replace}, "haar", 3, "modwt")
names(ln_income{replace}.haar) <- c("d1", "d2", "d3")

How do I run this for loop so that it generates something like this:

(in round 1):

ln_income1.haar <- mra(ln_income1, "haar", 3, "modwt")
names(ln_income1.haar) <- c("d1", "d2", "d1")

(in round 2):

ln_income2.haar <- mra(ln_income2, "haar", 3, "modwt")
names(ln_income2.haar) <- c("d1", "d2", "d3")

(then the same for round 100)

ln_inome100.haar <- mra(ln_inome100, "haar", 3, "modwt")
names(ln_inome100.haar) <- c("d1", "d2", "d3")

Thanks! /Pelle

John Coleman
  • 46,420
  • 6
  • 44
  • 103
Pelle
  • 1

1 Answers1

0

Thanks for generous and excellent answers, but I think that d1, d2, d3 were misunderstood (since I did not realize that d1,d2,d3... usually is used to denote different data frames). At my current beginner skill level I am totally fine with anything that works.

Actually, d1, d2, d3 are "numeric" wavelet decomposed variables (that is different wavelet time-scaled variables for each "numeric" variable).

There are 3 of these (d1,d2,d3) in ln_income1, 3 of these (d1,d2,d3) in ln_income2 etc,...,3 of these (d1,d2,d3) in ln_income100.

Maybe it is easiest for everyone if I give you the code I am using (here with the wage variables).

In the imported Stata file emilija3.dta I have the variables wage1, wage2,...,wage100.

In order to be even clearer, I have added a suffix to the d1, d2, and d3, that is d1_wage1, d2_wage1, d3_wage1, d1_wage2, d2_wage2, d3_wage2...... d1_wage100, d2_wage100, d3_wage100.

All of these wavelet transformed "numerics" can be saved in the same data frame. That is, this is just different time-scales of each variable.

Here is what I am trying to do (did it for wage this time, instead of ln_income):

   # wage1
   library(foreign)
   edata <- read.dta("c://test//emilija3.dta")
   wage1=edata$wage1
   library(waveslim)
   wage1.haar <- mra(wage1, "haar", 3, "modwt")
   names(wage1.haar) <- c("d1", "d2", "d3")
   d1_wage1=wage1.haar$d1
   d2_wage1=wage1.haar$d2
   d3_wage1=wage1.haar$d3
   d1_wage1
   d2_wage1
   d3_wage1
   ####################
   # wage2
   library(foreign)
   edata <- read.dta("c://test//emilija3.dta")
   wage2=edata$wage2
   library(waveslim)
   wage2.haar <- mra(wage2, "haar", 3, "modwt")
   names(wage2.haar) <- c("d1", "d2", "d3")
   d1_wage2=wage2.haar$d1
   d2_wage2=wage2.haar$d2
   d3_wage2=wage2.haar$d3
   d1_wage2
   d2_wage2
   d3_wage2
   ####################
   # wage3
   library(foreign)
   edata <- read.dta("c://test//emilija3.dta")
   wage3=edata$wage3
   library(waveslim)
   wage3.haar <- mra(wage3, "haar", 3, "modwt")
   names(wage3.haar) <- c("d1", "d2", "d3")
   d1_wage3=wage3.haar$d1
   d2_wage3=wage3.haar$d2
   d3_wage3=wage3.haar$d3
   d1_wage3
   d2_wage3
   d3_wage3
   ####################
   #...
   #...
   #...
   ####################
   # wage100
   library(foreign)
   edata <- read.dta("c://test//emilija3.dta")
   wage100=edata$wage100
   library(waveslim)
   wage100.haar <- mra(wage100, "haar", 3, "modwt")
   names(wage100.haar) <- c("d1", "d2", "d3")
   d1_wage100=wage100.haar$d1
   d2_wage100=wage100.haar$d2
   d3_wage100=wage100.haar$d3
   d1_wage100
   d2_wage100
   d3_wage100
   ####################

Is there a loop (or something more elegant if you like) that can solve this so that I do not need to do this 100 times for every variable?

Many thanks from a beginner in R. I am not concerned about the speed of the "program".

All answers are welcome!

P