How to do stuff in R. Part 2.

Continuing from how to do stuff in R, following the twotorials.

7) Data tables in R
I can’t quite decide if he is a bit crazy, but it’s fun.
> w<-data.frame(names=c("Birgit", "Andreas", "Thomas"), ability=c(100,200,300)) > w
names ability
1 Birgit 100
2 Andreas 200
3 Thomas 300
> w[1:2,]
names ability
1 Birgit 100
2 Andreas 200
> mean(w[,2])
[1] 200
> nrow(w)
[1] 3

8) Install and load a package in R
install.packages("xlsx")
library("xlsx")

Okay.

9) Function + help files
Learned the seq function (I did mention that this was going to come soon in last post, didn’t I?)
well, typing
?seq
leads to those examples:
> seq(0, 1, length.out=11)
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
> seq(stats::rnorm(20)) # effectively 'along'
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> seq(1, 9, by = 2) # matches 'end'
[1] 1 3 5 7 9
> seq(1, 9, by = pi) # stays below 'end'
[1] 1.000000 4.141593 7.283185
> seq(1, 6, by = 3)
[1] 1 4
> seq(1.575, 5.125, by=0.05)
[1] 1.575 1.625 1.675 1.725 1.775 1.825 1.875 1.925 1.975 2.025 2.075 2.125
[13] 2.175 2.225 2.275 2.325 2.375 2.425 2.475 2.525 2.575 2.625 2.675 2.725
[25] 2.775 2.825 2.875 2.925 2.975 3.025 3.075 3.125 3.175 3.225 3.275 3.325
[37] 3.375 3.425 3.475 3.525 3.575 3.625 3.675 3.725 3.775 3.825 3.875 3.925
[49] 3.975 4.025 4.075 4.125 4.175 4.225 4.275 4.325 4.375 4.425 4.475 4.525
[61] 4.575 4.625 4.675 4.725 4.775 4.825 4.875 4.925 4.975 5.025 5.075 5.125
> seq(17) # same as 1:17, or even better seq_len(17)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

10) How to read csv files

If you are lazy like me, just use this link to get the csv. Googling it will take you to a quite useful page, aka R class notes. Just in case. I copy-pasted it to my Desktop. Shift+Rightclick+Copy Path to put the path into the clipboard, just in case..

Reading it gives you a list:


> read.csv("C:/Users/Birgit/Desktop/hsb2.csv")
id female race ses schtyp prog read write math science
1 70 male white low public general 57 52 41 47
2 121 female white middle public vocation 68 59 53 63
3 86 male white high public general 44 33 54 58
4 141 male white high public vocation 63 44 47 53
5 172 male white middle public academic 47 52 57 53



> x<-read.csv("C:/Users/Birgit/Desktop/hsb2.csv")
> head(x)
id female race ses schtyp prog read write math science socst
1 70 male white low public general 57 52 41 47 57
2 121 female white middle public vocation 68 59 53 63 61
3 86 male white high public general 44 33 54 58 31
4 141 male white high public vocation 63 44 47 53 56
5 172 male white middle public academic 47 52 57 53 61
6 113 male white middle public academic 44 52 51 63 61
> tail(x)
id female race ses schtyp prog read write math science socst
195 179 female white middle private academic 47 65 60 50 56
196 31 female asian middle private general 55 59 52 42 56
197 145 female white middle public vocation 42 46 38 36 46
198 187 female white middle private general 57 41 57 55 52
199 118 female white middle public general 55 62 58 58 61
200 137 female white high public academic 63 65 65 53 61
>

> y<-read.csv("http://www.ats.ucla.edu/stat/r/notes/hsb2.csv")
> getwd()
[1] "C:/Users/Birgit/Documents"
> setwd("C:/Users/Birgit/Desktop")
> y<-read.csv("hsb2.csv")

11) How to use logic/ifs

> x<-T|F
> x
[1] TRUE
> if(x){print("Hallo")}
[1] "Hallo"
> if(!x){print("Hallo")}

Nothing too exciting.

I’m getting a bit bored with this. Kinda. But I’ll finish some more, soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: