Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using do loops in R to create new variables

Tags:

r

sas

I'm a long time SAS programmer looking to make the jump to R. I know R isn't all that great for variable re-coding but is there a way to do this with do loops.

If I have a lot of variables named a_1 a_2...a_100, b_1 b_2 ... b_100 and I want to create new variables c_1 c_2 ... c_100 where c_i = a_i + b_i. Is there a way to do this without 100 statements?

In SAS I would simply use:

%do i=1 %to 100;
c_&i = a_&i + b_&i;
%end;

Thanks!

like image 539
user1346861 Avatar asked Mar 19 '26 03:03

user1346861


2 Answers

SAS uses a rudimentary macro language, which depends on text replacement rather than evaluation of expressions like any proper programming language. Your SAS files are essentially two things: SAS commands, and Macro expressions (things starting with '%'). Macro languages are highly problematic and hard to debug (for example, do expressions within expressions get expanded? Why do you have to do "&&x" or even "&&&x"? Why do you need two semicolons here?). It's clunky, and inelegant compared to a well-designed programming language that is based on a single syntax.

If your a_i variables are single numbers, then you should have made them as a vector - e.g:

> a = 1:100
> b = runif(100)

Now I can get elements easy:

> a[1]

and add up in parallel:

> c = a + b

You could do it with a loop, initialising c first:

> c = rep(0,100)
> for(i in 1:100){
   c[i]=a[i]+b[i]
   }

But that would be sloooooow.

Nearly every R beginner asks 'how do I create a variable a_i for some values of i', and then shortly afterwards they ask how to access variable a_i for some values of i. The answer is always to make a as either a vector or a list.

like image 192
Spacedman Avatar answered Mar 21 '26 17:03

Spacedman


I suspect that if you have one hundred variables a_1, a_2, ..., a_100, all of your variables are related. In fact, if you want to do

c_1 = a_1 + b_1

then a, b, c are related. Therefore, I recommend that you combine all of your variables into a single data frame, where one column is a and another is b.

The question is how do you combine your variables in a sensible way. However, to give a useful answer, can you tell us how these variables are created?


Perhaps this isn't suitable, for your case. If not, a bit more information would be useful.

like image 28
csgillespie Avatar answered Mar 21 '26 15:03

csgillespie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!