Concepts
- Data types : character, numeric, integer, complex, logical
- A vector can only contain objects of the same class
- List is represented as a vector but can contain objects of different classes
- Numbers in R are generally represented as numeric objects
- If you explicitly want an integer, you need to specify the L suffix
- Ex : entering 1 will be treated as a numeric object and 1L will be treated as an integer
- R objects can have attributes - names, dimensions, class, other user defined attributes
- x <- -="" 1:20="" create="" integer="" is="" operation="" sequences="" span="" the="" to="" used="">->
- The function c() is used to create vectors of objects
- Objects can be co-erced from one class to another using as.* function
- x <- 1:20="" as.character="" span="" x="">->
- Non sensical co-ersion results in NA
- Matrices are vectors with dimension attribute.
- Matrices are created columnwise, so entries start at the upper left corner
- Matrices can also be created from vectors by adding the dimension attribute.
- x <- 1:10="" c="" dim="" m="" nbsp="" span="">->
- Matrices can also be created by column binding or row binding
- x <- 10:12="" 2:4="" cbind="" m="" nbsp="" rbind="" span="" x="" y="">->
- Factors - categorical data
- Missing values : NA and NAN. NA can be integer NA or character NA and they have classes.
- NAN value is NA but the converse is not true
- Data Frames
- Special type of list where every element of the list has to be the same length
- Each element of the list can be thought of as a column and the length of each element of the list is the number of rows
- data frames can store different classes of objects in each column. Matrixes all elemets have to be of the same class
- Data frames also have special attributes called row.names
- Data frames are created by read.table() or read.csv()
- R Objects can have names
- m <- matrix="" nrow="2,ncol=2)</span">->
- dimnames(m) <- a="" b="" c="" d="" list="" span="">->
- sd
- sd
Examples
- How to get a list of available packages in R ?
- How to install packages ?
install.packages("slidify"); install.packages(c("slidify", "ggplot"));
How do you load R packages ? After loading a package the functions loaded in the package will be attached to the top of the search list
- How do you load the package in R ? library("slidify")
- sd
1) How to read a csv file in R ?
1
| data<- code=""> |
2) How to display the first n lines of the file ?
1
| head(data,n) : The default value of n is 6. |
3) How to display the last n lines of the file ?
1
| tail(data,n) |
4) Calculate missing values in all the columns in the data set ?
1
| colSums(data) |
Other functions that can be used for this purpose are sapply and apply.
5) Calculate the mean of a column without the missing values ?
1
2
3
4
5
6
7
8
9
| colMeans(data,na.rm=TRUE) Ozone Solar.R Wind Temp Month Day 42.129310 185.931507 9.957516 77.882353 6.993464 15.803922 colMeans(data) Ozone Solar.R Wind Temp Month Day NA NA 9.957516 77.882353 6.993464 15.803922 colMeans(data["Ozone"],na.rm=TRUE) Ozone 42.12931 |
6) Extract the subset of rows of the data frame where Ozone values are above 31 and Temp values are above 90. What is the mean of Solar.R in this subset?
1
2
3
| colMeans(subset(data,(Ozone>31 & Temp>90))) Ozone Solar.R Wind Temp Month Day 89.5 212.8 5.6 93.4 8.2 14.5 |
7) Find the mean temperature in the Month of n ?
1
2
3
| colMeans(subset(data,Month==n)) Ozone Solar.R Wind Temp Month Day NA 190.16667 10.26667 79.10000 6.00000 15.50000 |
Additional Resources :
1) Filling in nas with column medians in R
1) Filling in nas with column medians in R
No comments:
Post a Comment