#create a vector with fruit
fruit <- c("apple", "orange", "banana", "pear", "cherry", "plum", "grape")
# look for a fruit in the vector
"pear"%in% fruit
## [1] TRUE
#this will result in true
#check to see which position this is in
fruit=="pear"
## [1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE
#this results in boolean vector of true and false - TRUE indicates which one was pear
#set a vector to this result
location<- fruit=="pear"
location
## [1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE
x <- fruit[location]
x
## [1] "pear"
#let's find out which position it is located
# we will use a for loop
num<- length(fruit)
#first determine how many fruits there are
#first let's just print out the fruits
for (fruit.type in fruit) {
print(fruit.type)
}
## [1] "apple"
## [1] "orange"
## [1] "banana"
## [1] "pear"
## [1] "cherry"
## [1] "plum"
## [1] "grape"
#now let's look for the fruit pear
for (fruit.type in fruit) {
print(fruit.type)
if (fruit.type == "pear")
{print("found Fruit")}
}
## [1] "apple"
## [1] "orange"
## [1] "banana"
## [1] "pear"
## [1] "found Fruit"
## [1] "cherry"
## [1] "plum"
## [1] "grape"
#now comment out the print fruit.type
for (fruit.type in fruit) {
print(fruit.type)
if (fruit.type == "pear")
{print("found Fruit")}
}
## [1] "apple"
## [1] "orange"
## [1] "banana"
## [1] "pear"
## [1] "found Fruit"
## [1] "cherry"
## [1] "plum"
## [1] "grape"
# notice all we got was the statement "found fruit"
#but what about the location
#let's try to keep track of the location
#we will use a counter
countr <- 1 #set the counter to one
for (fruit.type in fruit) {
# print(fruit.type)
if (fruit.type == "pear")
{ found.location <- countr # save the value of the counter
print("found Fruit")}
countr<-countr+1 #increment the counter by 1 to keep counting
}
## [1] "found Fruit"
found.location #this will display the location
## [1] 4
#notice that we only set the value of found.location when we found the match.
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
#we will now do this relying on the length - we set num to that value
countr <- 1 #set the counter to one
for (i in 1:num) {
# notice that we are referencing into the vector using []
# i will have values from 1 to 7 since there are 7 fruits
if (fruit[i] == "pear")
{ found.location <- countr # save the value of the counter
print("found Fruit")}
#this point we no longer need countr since i will have the same value
countr<-countr+1 #increment the counter by 1 to keep counting
}
## [1] "found Fruit"
found.location #this will display the location
## [1] 4
#the next code removes countr and replaces its use with i
for (i in 1:num) {
# notice that we are referencing into the vector using []
# i will have values from 1 to 7 since there are 7 fruits
if (fruit[i] == "pear")
{ found.location <- i # save the value of i
print("found Fruit")}
#this point we no longer need countr since i will have the same value
}
## [1] "found Fruit"
found.location #this will display the location
## [1] 4
#why is this of value?
#well sometimes we need to know where things are located
#for instances now we can reference the specific location in fruit
fruit[found.location] # this of course displays pear
## [1] "pear"
#but look at what the following will do
fruit[c(-found.location)] #this displays everything but pear
## [1] "apple" "orange" "banana" "cherry" "plum" "grape"
#Something to note is that even though the location has been found the for loop will continue
#let's look at how we can use a different type of loop
#we will use the repeat - we will need to use a countr again
cnt <- 1 #i changed the name of the counter - too many letters to type
repeat {
if(fruit[cnt]=="pear")
{
found.location = cnt
break }
cnt <- cnt+1
}
found.location #same value but we stop looking
## [1] 4
#we will now create a data frame that combines this information without using a loop
new.data<-cbind(fruit,location) #remember location has the values of TRUE or FALSE where the match occurred
new.data
## fruit location
## [1,] "apple" "FALSE"
## [2,] "orange" "FALSE"
## [3,] "banana" "FALSE"
## [4,] "pear" "TRUE"
## [5,] "cherry" "FALSE"
## [6,] "plum" "FALSE"
## [7,] "grape" "FALSE"
newnew.data<-cbind(fruit, as.numeric(location)) #Hm. I thought we just did this
newnew.data #notice the difference - this can happen because TRUE and FALSE can take on the values of 1 and 0
## fruit
## [1,] "apple" "0"
## [2,] "orange" "0"
## [3,] "banana" "0"
## [4,] "pear" "1"
## [5,] "cherry" "0"
## [6,] "plum" "0"
## [7,] "grape" "0"
class(newnew.data) #notice that the class of this is a matrix
## [1] "matrix" "array"
str(newnew.data) # that means that the number we are seeing are treated as text - a matrix can only have one type
## chr [1:7, 1:2] "apple" "orange" "banana" "pear" "cherry" "plum" "grape" ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:2] "fruit" ""
#let's use the data frame and see the difference
#we create a data frame using the same method for cbind but make sure that we don't have factors
fruit.df<-data.frame(fruit=fruit,found=as.numeric(location), stringsAsFactors = FALSE )
fruit.df
## fruit found
## 1 apple 0
## 2 orange 0
## 3 banana 0
## 4 pear 1
## 5 cherry 0
## 6 plum 0
## 7 grape 0
class(fruit.df) #look at the difference
## [1] "data.frame"
str(fruit.df) #look at the structure
## 'data.frame': 7 obs. of 2 variables:
## $ fruit: chr "apple" "orange" "banana" "pear" ...
## $ found: num 0 0 0 1 0 0 0
#####
#####now on your own add more fruits to the fruit vector
#be sure and duplicate some fruits
#now generate the matrix and the dataframe as shown above
## put your answer below
##in this section we will add a column to the original data frame it contains calories for the seven fruits
cal<-c(101, 90, 105, 96, 103, 91, 101)
##you will need to add calories for the fruits you added above
new.cal.df<-data.frame(fruit=fruit.df$fruit,cal= cal)
#the following lines examine the data frame
new.cal.df
## fruit cal
## 1 apple 101
## 2 orange 90
## 3 banana 105
## 4 pear 96
## 5 cherry 103
## 6 plum 91
## 7 grape 101
class(new.cal.df)
## [1] "data.frame"
str(new.cal.df)
## 'data.frame': 7 obs. of 2 variables:
## $ fruit: chr "apple" "orange" "banana" "pear" ...
## $ cal : num 101 90 105 96 103 91 101
new.cal.df$cal
## [1] 101 90 105 96 103 91 101
# let's look for fruits that have calories greater than or equal to 100
res.df<-data.frame(new.cal.df$fruit, new.cal.df$cal>=100)
res.df
## new.cal.df.fruit new.cal.df.cal....100
## 1 apple TRUE
## 2 orange FALSE
## 3 banana TRUE
## 4 pear FALSE
## 5 cherry TRUE
## 6 plum FALSE
## 7 grape TRUE
# let's get a subset of this
redcd.df<-subset(new.cal.df, cal >= 100)
redcd.df
## fruit cal
## 1 apple 101
## 3 banana 105
## 5 cherry 103
## 7 grape 101
### do this for calories less than 100
###your answer below
## determine how many fruits have calories > 100
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.