R provided us with the way to search through data frame in similar fashion like with databases. Basically, you can provide some search criteria and get data out of data frame based on that very same criteria you have provided.
If we have data frame like this one:
data <- data.frame(names=c("John", "Steve", "Mick", "Brian"), age= c(14,19,25,20))
If we want to search for person whose age is greater than 19, we could do something like this:
data$name[data$age > 19]
What is happening in here? data$name means that we are going to search through data frame called data through column name. Condition for this search is within angle brackets. We are searching for those elements of column name whose value for age is greater than 19.
The only new thing in here is $. This symbol is used to split object that holds data from its elements; like in this case data frame and its column.
But not only that you can provide single search criteria, you could provide multiple in this form:
data$name[data$age>10 & data$age<20]
You can get result from multiple of columns if you want. Check this one:
c(data$name, data$age)[data$age>10 & data$age<20]
Now, we are going to receive result from both columns.
For video tutorial, link below.
No comments:
Post a Comment