Unique entries

Make vector entries unique with unique

length(iris$Sepal.Length)
## [1] 150
length(unique(iris$Sepal.Length))
## [1] 35

Count occurrences

Count occurrences of entries with table

table(iris$Species)
## 
##     setosa versicolor  virginica 
##         50         50         50

Aggregate data

Compute aggregate statistics with aggregate

aggregate(iris[,1:4], by=list(iris$Species), FUN=mean, na.rm=TRUE)
##      Group.1 Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1     setosa        5.006       3.428        1.462       0.246
## 2 versicolor        5.936       2.770        4.260       1.326
## 3  virginica        6.588       2.974        5.552       2.026

Intersect data

Compute intersect between two vectors with %in%

month.name %in% c("May", "July")
##  [1] FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE

Merge data frames

Join two data frames by common field entries with merge (here row names by.x=0). To obtain only the common rows, change all=TRUE to all=FALSE. To merge on specific columns, refer to them by their position numbers or their column names.

frame1 <- iris[sample(1:length(iris[,1]), 30), ]
frame1[1:2,]
##     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 79           6.0         2.9          4.5         1.5 versicolor
## 124          6.3         2.7          4.9         1.8  virginica
dim(frame1)
## [1] 30  5
my_result <- merge(frame1, iris, by.x = 0, by.y = 0, all = TRUE)
dim(my_result)
## [1] 150  11



Previous page.Previous Page                     Next Page Next page.