- Create a data frame dfthat contains the following variables for at least four observations:
- name: name of at least four friends or acquaintances
- age: the age of those persons
- size: the height of those persons in cm
- city: Place of residence of those persons (city)
Click here to see the answer
df <- data.frame(
  name = c("Anna", "Otto", "Natan", "Ede"), 
  age  = c(66, 53, 22, 36),
  size = c(170, 174, 182, 180),
  city = c("Hamburg", "Berlin", "Berlin", "Cologne")
  )
df
##    name age size    city
## 1  Anna  66  170 Hamburg
## 2  Otto  53  174  Berlin
## 3 Natan  22  182  Berlin
## 4   Ede  36  180 Cologne- Examine the dimensionality, structure and statistical summary of your data frame df!
Click here to see the answer
dim(df)
## [1] 4 4
str(df)
## 'data.frame':    4 obs. of  4 variables:
##  $ name: Factor w/ 4 levels "Anna","Ede","Natan",..: 1 4 3 2
##  $ age : num  66 53 22 36
##  $ size: num  170 174 182 180
##  $ city: Factor w/ 3 levels "Berlin","Cologne",..: 3 1 1 2
summary(df)
##     name        age             size            city  
##  Anna :1   Min.   :22.00   Min.   :170.0   Berlin :2  
##  Ede  :1   1st Qu.:32.50   1st Qu.:173.0   Cologne:1  
##  Natan:1   Median :44.50   Median :177.0   Hamburg:1  
##  Otto :1   Mean   :44.25   Mean   :176.5              
##            3rd Qu.:56.25   3rd Qu.:180.5              
##            Max.   :66.00   Max.   :182.0- Index the second column with simple square brackets []and save the output asdf.subset! Which class does the output belong to?
Click here to see the answer
df.subset <- df[2]
df.subset
##   age
## 1  66
## 2  53
## 3  22
## 4  36
class(df.subset)
## [1] "data.frame"- Index the variable agewith double square brackets[]and save the output asage.persons. Which class does the output belong to?
Click here to see the answer
age.persons <- df[["alter"]]
# oder
age.persons <- df[[2]]
age.persons
## [1] 66 53 22 36
class(age.persons)
## [1] "numeric"- Add the variable weightin kg to the data framedf!
Click here to see the answer
df$weight <- c(115, 110.2, 95, 87)
df
##    name age size    city weight
## 1  Anna  66  170 Hamburg  115.0
## 2  Otto  53  174  Berlin  110.2
## 3 Natan  22  182  Berlin   95.0
## 4   Ede  36  180 Cologne   87.0- Add another observation (person) to your df!
Click here to see the answer
new.person <- data.frame(
  "name"   = "Anna",
  "age"    = 32,
  "size"   = 174,
  "weight" = 63,
  "city"   = "Hamburg"
)
df <- rbind(df, new.person)
df
##    name age size    city weight
## 1  Anna  66  170 Hamburg  115.0
## 2  Otto  53  174  Berlin  110.2
## 3 Natan  22  182  Berlin   95.0
## 4   Ede  36  180 Cologne   87.0
## 5  Anna  32  174 Hamburg   63.0- Calculate the mean value of the variable ageand save the result asages.mean!
Click here to see the answer
ages.mean <- mean(df$age)- Index all observations (persons) that are older than the average ages.mean!
Click here to see the answer
df[df$age > ages.mean, ]- Index all persons, which are lighter than 100 kg AND at least 180 cm tall!
Click here to see the answer
df[df$weight = 180, ]
##    name age size    city weight
## 3 Natan  22  182  Berlin     95
## 4   Ede  36  180 Cologne     87
# or
subset(df, df$weight = 180)
##    name age size    city weight
## 3 Natan  22  182  Berlin     95
## 4   Ede  36  180 Cologne     87