Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
When I say Ease of Use Improved, I mean you can simply copy, paste and run the codes in this post, without referring to other places, without downloading a data file and read it from R. This is how I like a blog article to be. You don’t need to read the whole article. You just need to Ctrl+F what your need and copy the codes there and run it.
I use R in Windows and sometimes Linux. The version is 2.13.0. The following scripts should be applicable to other versions.
Read a File to a Table
Hmm.. You can’t copy and run this in your system, since you don’t have that file. congold is a table, the first argument of read.table() is the path of the file. In Windows, you should use “/” in the path instead of “\”.
Boxplot
t = rep(c(1,2),c(5,5))
boxplot(d~t)
Get subset
subset(df,col2==2)
Find out how many unique items in a list
length(unique(a))
Viewing Several Graphs
In Windows
In Linux
In Mac
Delete Columns by Names
df <- df[,-which(names(df) %in% c("z","t"))]
An easier way:
df <- subset(df, select=-c(z,t))
Actually, it is done by selecting the columns you want. So we have the following:
Select Columns by Names
df[, c("x","y")]
subset(df, select=c(x,y))
Print out Column Names
names(df)
Change Column Names
names(df)[[1]]="newNameForColumn1"
names(df)=c("newNameForColumn1", "newNameForColumn2", "newNameForColumn3","newNameForColumn4")
names(df)[which(names(df)=="y")]= "NewNameOf_y"
Reduction Plot
x = 1:100
y = rnorm(100)
xyplot(x~y, type=c("r","p"))
Finding out 95%th, 99%th of Each Category
x = rep(c(1,2),50)
y = rnorm(100)
summaryBy(y~x, data=df, FUN=function(x){quantile(x,c(0.95,0.99))})
y = rnorm(100)
aggregate(y~x, data = df, function(x){quantile(x,0.95)})
aggregate(y~x, data = df, function(x){quantile(x,0.99)})
Get Median of Each Factor in a data frame (each type has many rows)
y = rnorm(100)
aggregate(y~x, data = df, median)
To count rows or columns
nrow(df)
ncol(df)
Create empty matrix or vector
Replace data in data frame
selected = tmp == 2
selected
tmp[selected] = 22
tmp
Convert Factor to Number
size
as.numeric(size)
levels(size)[size]
as.numeric(levels(size)[size])
Change the order of colums
df
df = subset(df, select=c(c,b,a))
df
Order Data Frame
df = df[order(df$b),]
df
df[order(df$c,df$b),]
Too much to organize from my note…
Maybe I’ll pick it up later, nor not….
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.