In this page, I put some commands/ways that I found useful for analyzing your data more quickly and effectively. If you are a beginner of R, you should take a look at the intro pdf provided by R project first. There are many online materials for R, so you should do online search. You may find other useful tips in Rtips (http://pj.freefaculty.org/R/Rtips.html).
For example, if you want to install car package,
You will be asked to select one of the sites to download the package. And then R will just do everything for you.
For example, if you want to include car package, you can do
R will include the other packages which the package you are going to include has dependency on.
You can know where the current working directory is by
If you want to change the working directory, you can do like
You can read a csv file by
If your csv file does not have the header (i.e., the first row representing the names of variables), you can do header=F. The imported data (dat) is a dataframe.
You can write a csv file by
Dataframe is a very useful form to represent the data in R. It is like a table, but you can do different operations on it.
You can use data.frame to create a dataframe.
You can also pick up a specific column by using $.
If you want to take a row, you can do this.
You can also do like data[,1] to specify a column (in this case, the first column). Please note that the index starts 1, not 0 like an array or list in many programming lauguages.
You can also add a column at the end by doing this.
You can also remove a column.
You can use to see or change the names of columns.
You can also add a row at the end by doing this.
You can remove any specific row by putting “-” in front of the index. In this case, we are removing the sixth row (which we've just added).
summary() is a very useful function to know the general information of the variable.
You can see various stats (e.g., the mean, median, min and max) with summary(). Because G is a factor (or nominal data), you only see the count. This is a common way to use summary, but it also provides different information depending on the variable you put in the function. So if you want to get some general information about the variable you have, you should try summary().
You can use by() function to apply a specific function to different groups in a dataframe. In the following example, I apply summary() for Group M and Group F.
In the following examples, I use a dataframe, but you can use functions in Apply family with a matrix or list.
You can do calculations for each row or column by using apply() function. The following example calculates the sum for each row and column.
You can apply a specific function to each column by using lapply or sapply function.