r-commands

# r-commands - Math 127B Basic R R commands Mrinal Raghupathi...

This preview shows pages 1–3. Sign up to view the full content.

Math 127B, Basic R, R commands Mrinal Raghupathi This is a list of the commands we use most often in R. The list is not comprehensive, but it covers most of the things we need. 1 Making lists, data frames Basic list creation Make a list and assign it to a name x <- c(1,2,3) Sequence seq(start, stop, step) creates the list of numbers beginning at start, in steps of size step until it reaches stop . > x <- seq (3 ,15 ,4) > x [1] 3 7 11 15 Repetition rep(number, times) produces a list with number repeated times times. > x <- rep (5 ,7) > x [1] 5 5 5 5 5 5 5 Dollar The \$ sign can be used to extract a properties. For example the value returned from the lm command, or the column of a data frame. Data frame data.frame() can be used to make a data frame. Supply the names of the columns and the corresponding values with options of the form name = value . > men <- c(45, 86, 12) > women <- c(30 ,105 ,21) > both <- data.frame(gents = men , ladies = women) > both gents ladies 1 45 30 2 86 105 3 12 21 > both\$ladies [1] 30 105 21 You can assign a list as a new column of the data frame by using the \$ operation and assigning the list to it. 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Statistics Frequency tables table(x) produces the frequencies for entries in x . > grades <- c(’a’, ’b’, ’a’, ’b’,’a’,’a’,’a’,’a’,’b’) > length(grades) [1] 9 > table(grades) x a b 6 3 Using two lists you can get the cross tabulation. For example here are the subject areas in which the grades were recieved. > subjects <- c(’math ’, ’math ’, ’physics ’, ’physics ’ ,’math ’, ’math ’, ’physics ’, ’physics ’, ’math ’) > table(subjects) subjects math physics 5 4 > table(grades , subject) subject grades math physics a 3 3 b 2 1 Correlation cor(x,y)
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern