Variables, or columns, of a data frame can be selected with the $ operator, and the resulting object is a vector. Mydata <- ame(patient=c("Smith", "Jones", "Williams"),Įxtract the 2nd, 5th, and 10th rows of the variable math in the dat_csv data set. # use ame() to create a data frame manually Omitting rows or columns specifies all rows and columns, respectively. Use vectors to subset multiple rows/columns. With a two-dimensional structure, data frames can be subset with matrix notation. # 199 57 female middle public academic 71 65 72 66 56
# 198 68 male middle public academic 73 67 71 63 66 # id female ses schtyp prog read write math science socst # 2 108 male middle public general 34 33 41 36 36 not enrolled # 1 45 female low public vocation 34 35 41 29 26 not enrolled # id female ses schtyp prog read write math science socst honors View(dat_csv)įor large data files, use head() and tail() to look at a specified number of rows at the begininning or end of a dataset, respectively. In RStuido, clicking on a dataset in the Environment pane will View() it. Use View() on a dataset to open a spreadsheet-style view of a dataset. Viewing data as a spreadsheet with View(), head() and tail() Try creating the vector (2,2,1,1) in at least two different ways. # the second argument is number of repetitionsĬreate the vector (4,5,6) in three different ways using c(), seq(), and the : operator. The expression m:n will generate a vector of integers from m to n # first argument to rep is what to repeate
To create vectors with a predictable sequence of elements, use rep() for repeating elements and seq() for sequential elements. The c() function combines values of common type together to form a vector. Unless otherwise specified, all coding instructions for this seminar should be entered into the script editor. Save your R script with the name mycode.R. Make sure to periodically save your scripts. This will advance the cursor to the next command, where you can hit Ctrl-Enter again to run it, advancing the cursor to the next command…Įxecute the two commands in the script editor using the keyboard shortcuts.
To execute code directly from the script editor, place the cursor inside the command (or highlight the entire command), and then hit Ctrl-Enter (on PCs, use Command-Enter on Macs). When the tab-completion window appears with a list of possible commands, hit Tab to choose log(). Write the code 1 + 2 in the script editor. If you do not see the script editor already open, open it now by selecting File > New File > R Script. The script editor features the same tab-code-completion and function help as the console, as well as syntax highlighting. We can also issue R commands directly from the editor. Instead, we use the script editor to save our commands as a record of the steps we took to analyze our data. Most R programs written for data analysis consists of many commands, making entering code line-by-line into the console inefficient. Descriptive statistics for continuous variables.
Adding data columns by merging on a key variable *.Appending observations (appending by rows).Subsetting rows of a data frame with filter().Viewing data as a spreadsheet with View(), head() and tail().Conditional selection - subsetting by value.R programming 2: Functions and help files.