Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rewrite this Stata code in R?

Tags:

r

stata

One of the things Stata does well is the way it constructs new variables (see example below). How to do this in R?

foreach i in A B C D {  
    forval n=1990/2000 {  
       local m = 'n'-1  
       # create new columns from existing ones on-the-fly  
       generate pop'i''n' = pop'i''m' * (1 + trend'n')  
   }  
}  
like image 445
prasoonsharma Avatar asked Feb 17 '11 02:02

prasoonsharma


People also ask

Can R run Stata code?

Running R from StataThe trick to running R from within your do-file is first to save the data you want to pass to R, then call the . R file with the commands you want to run in R (the “R script”), then—if necessary—reload the R output into Stata.

Is R similar to Stata?

R and Stata are very different with regard to storing, accessing, and modifying data and variables. Stata only loads one data set at a time. R stores all object including data as objects and can have an arbitrary number loaded at once. Below I create an interaction term between x and z and a discrete version of x .

What coding language is used in Stata?

The software was originally intended to compete with statistical programs for personal computers such as SYSTAT and MicroTSP. Stata was written, then as now, in the C programming language, initially for PCs running the DOS operating system. The first version was released in 1985 with 44 commands.

What is R mean in Stata?

Notice that instead of using the actual value of the mean of read in this command, we used the name of the returned result (i.e. r(mean)), Stata knows when it sees r(mean) that we actually mean the value stored in that system variable.


1 Answers

DONT do it in R. The reason its messy is because its UGLY code. Constructing lots of variables with programmatic names is a BAD THING. Names are names. They have no structure, so do not try to impose one on them. Decent programming languages have structures for this - rubbishy programming languages have tacked-on 'Macro' features and end up with this awful pattern of constructing variable names by pasting strings together. This is a practice from the 1970s that should have died out by now. Don't be a programming dinosaur.

For example, how do you know how many popXXXX variables you have? How do you know if you have a complete sequence of pop1990 to pop2000? What if you want to save the variables to a file to give to someone. Yuck, yuck yuck.

Use a data structure that the language gives you. In this case probably a list.

like image 101
Spacedman Avatar answered Nov 03 '22 12:11

Spacedman