- Create a data frame
df
that 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
<- data.frame(
df 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[2]
df.subset
df.subset## age
## 1 66
## 2 53
## 3 22
## 4 36
class(df.subset)
## [1] "data.frame"
- Index the variable
age
with double square brackets[]
and save the output asage.persons
. Which class does the output belong to?
Click here to see the answer
<- df[["alter"]]
age.persons
# oder
<- df[[2]]
age.persons
age.persons## [1] 66 53 22 36
class(age.persons)
## [1] "numeric"
- Add the variable
weight
in kg to the data framedf
!
Click here to see the answer
$weight <- c(115, 110.2, 95, 87)
df
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
<- data.frame(
new.person "name" = "Anna",
"age" = 32,
"size" = 174,
"weight" = 63,
"city" = "Hamburg"
)
<- rbind(df, new.person)
df
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
age
and save the result asages.mean
!
Click here to see the answer
<- mean(df$age) ages.mean
- Index all observations (persons) that are older than the average
ages.mean
!
Click here to see the answer
$age > ages.mean, ] df[df
- Index all persons, which are lighter than 100 kg AND at least 180 cm tall!
Click here to see the answer
$weight = 180, ]
df[df## 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