How would I write the below code more efficiently?

brb

How could I write the below code more efficiently (ie in less lines)?

I can't seem to define a function with a Year argument and write something like Data{Year} = read.csv('{Year}.csv') or Data{Year}${Year+1} = 0 etc...

## Load data ##

Data2011 = read.csv('2011.csv')
Data2012 = read.csv('2012.csv')
Data2013 = read.csv('2013.csv')
Data2014 = read.csv('2014.csv')

## Year dummies ##

Data2011$D2011 = 1 
Data2011$D2012 = 0 
Data2011$D2013 = 0 
Data2011$D2014 = 0 

Data2012$D2011 = 0 
Data2012$D2012 = 1 
Data2012$D2013 = 0 
Data2012$D2014 = 0 

Data2013$D2011 = 0 
Data2013$D2012 = 0 
Data2013$D2013 = 1 
Data2013$D2014 = 0 

Data2014$D2011 = 0 
Data2014$D2012 = 0 
Data2014$D2013 = 0 
Data2014$D2014 = 1 
Aaron left Stack Overflow

Well, I'd first read in the data in loop, and attach a new column with the year.

dat <- lapply(2011:2014, function(y) cbind(Year=y, read.csv(paste0(y, '.csv')))

Now, the most common use for dummies is when you're fitting a model, so I'm guessing you want to put all the data together.

dat <- do.call(rbind, dat)

Then in most model fitting you'd never make the dummies yourself, that's a job for the computer. You'd just make the variable of interest a factor and then R will do the right thing.

dat$Year <- factor(dat$Year)

That's all I'd normally do. But if for some reason I really wanted to make those dummies myself, I'd still let the computer do it, like this, and then add that to the data set.

dums <- model.matrix(~0+Year, data=dat)
dat <- cbind(dat, dums)

For the purpose of learning how to loop and access variables, you could also do something like this, where the [[ allows you to use a character string to access or create a variable name, and the *1 converts a boolean to 0/1.

for(y in unique(dat$year)) {
    dat[[paste0("Year", y)]] <- (dat$Year==y)*1
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android| How would I write this code to be more Object orientated?

From Dev

How can I write this CSS more efficiently?

From Dev

How can I write this CSS more efficiently?

From Dev

How would i write regex for 250 or more?

From Dev

How can I write my code more efficiently for use with different types of JavaFX GUI components?

From Dev

How can I write this code more elegantly?

From Dev

In the below java code, how can I ask the user to input how many shots they would like in each of their coffee cups (Given they order more than 1)?

From Dev

How would I make this code more compact/efficient?

From Dev

How would I write the Regex for this?

From Dev

More efficiently write if statements

From Dev

How to make this PHP code run more efficiently?

From Java

how can i write more simple code without new select?

From Dev

How do I write the jQuery in this code more efficient?

From Dev

How Can I Rewrite This More Efficiently

From Dev

How would I make this script more efficient?

From Dev

How to write validation for below code in jQuery?

From Dev

Is any idea how to write UT for below code?

From Dev

How to write pandas command more efficiently using for loop

From Dev

How would I write this C function in Rust?

From Dev

How would I properly write this query in django?

From Dev

How would I write NumPy argmode()?

From Dev

How would I write this VIM command for `sed`?

From Dev

How would I write this program in Python 3.3.2?

From Dev

How would I write a triangle function recursively?

From Dev

How would I write this in Javascript without jQuery?

From Dev

How can i write a piece of code that would check the amount of times a word has occured from the user input

From Dev

user enters 10 numbers into an array how would I write code to find that number and output what position it is in the array?

From Dev

How would I set value as zero for this below statement in mvc 4

From Dev

How would I stop a jquery method running below a certain width?

Related Related

  1. 1

    Android| How would I write this code to be more Object orientated?

  2. 2

    How can I write this CSS more efficiently?

  3. 3

    How can I write this CSS more efficiently?

  4. 4

    How would i write regex for 250 or more?

  5. 5

    How can I write my code more efficiently for use with different types of JavaFX GUI components?

  6. 6

    How can I write this code more elegantly?

  7. 7

    In the below java code, how can I ask the user to input how many shots they would like in each of their coffee cups (Given they order more than 1)?

  8. 8

    How would I make this code more compact/efficient?

  9. 9

    How would I write the Regex for this?

  10. 10

    More efficiently write if statements

  11. 11

    How to make this PHP code run more efficiently?

  12. 12

    how can i write more simple code without new select?

  13. 13

    How do I write the jQuery in this code more efficient?

  14. 14

    How Can I Rewrite This More Efficiently

  15. 15

    How would I make this script more efficient?

  16. 16

    How to write validation for below code in jQuery?

  17. 17

    Is any idea how to write UT for below code?

  18. 18

    How to write pandas command more efficiently using for loop

  19. 19

    How would I write this C function in Rust?

  20. 20

    How would I properly write this query in django?

  21. 21

    How would I write NumPy argmode()?

  22. 22

    How would I write this VIM command for `sed`?

  23. 23

    How would I write this program in Python 3.3.2?

  24. 24

    How would I write a triangle function recursively?

  25. 25

    How would I write this in Javascript without jQuery?

  26. 26

    How can i write a piece of code that would check the amount of times a word has occured from the user input

  27. 27

    user enters 10 numbers into an array how would I write code to find that number and output what position it is in the array?

  28. 28

    How would I set value as zero for this below statement in mvc 4

  29. 29

    How would I stop a jquery method running below a certain width?

HotTag

Archive