###QUESTION 10
###Topic: Sheet 8 Exercise 1
###Question: I dont really understand this components:
y<- c()
#and then further is y<- c(y, names(data)[i])
myfact <- function(data) {
y <-c()
for (i in 1:dim(data)[2]){
if (is.factor(data[,i]) == TRUE){
y<-c(y, names(data)[i])
}
}
return(y)
}
heartbeats <- read.table("C:/Users/Ingo/Documents/A-Uni/EES/3rd_semester/Rcourse2020_Tutoring/data/heartbeats.txt")
myfact(heartbeats)
## NULL
ht2 <- heartbeats
ht2$wghtcls <- as.factor(ht2$wghtcls)
myfact(ht2)
## [1] "wghtcls"
#my confusion is mostly why do I need to put y inside the vector?
#I can see is necessary because I tried doing it without it and it didnt work,
#but I still dont understand it. And also:
# We have to define that "y" variable as a vector, because it can be multiple elements right?
###Answer:
#maybe it becomes more clear with a simpler example.
#here we will simply add a new number to the end of a vector
x <- c()
for (i in 1:5) {
x <- c(x, i)
}
#First we need to define x, because in our for loop, we already attempt to assign x to something
#even though it doesn't exist yet.
#if you'd run the code:
# clean your R environment to remove x
rm(list=ls())
#for (i in 1:5) {
# x <- c(x, i)
# print(x)
#}
#without creating x first, you would get an error saying "Object 'x' not found
#What you`re doing in the code and the reason why x (or y in the example above) is inside the vector is that the
#new value will be added to the end, instead of just overwriting the previous one.
#if you print the x after each iteration you see that the current value of i gets added to the end of the vector
x <- c()
for (i in 1:5) {
print(x)
x <- c(x, i)
}
## NULL
## [1] 1
## [1] 1 2
## [1] 1 2 3
## [1] 1 2 3 4
print(x)
## [1] 1 2 3 4 5
#if the current value of x weren't in the assignment, you'd just overwrite x every round
x <- c()
for (i in 1:5) {
print(x)
x <- c(i)
}
## NULL
## [1] 1
## [1] 2
## [1] 3
## [1] 4
print(x)
## [1] 5
#depending on what you will do with the variable, you don't necessarily have to define it as a vector,
#you can also assign x to be NULL it will become a vector in the for loop anyway
x <- NULL
for (i in 1:5) {
print(x)
x <- c(x, i)
}
## NULL
## [1] 1
## [1] 1 2
## [1] 1 2 3
## [1] 1 2 3 4
print(x)
## [1] 1 2 3 4 5
#if you first define x as having a specific value (e.g. NA or 0) then this will actually be the first entry
x <- NA
for (i in 1:5) {
print(x)
x <- c(x, i)
}
## [1] NA
## [1] NA 1
## [1] NA 1 2
## [1] NA 1 2 3
## [1] NA 1 2 3 4
print(x)
## [1] NA 1 2 3 4 5