Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvements_Bagrat_Akopyan #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions checkHeight_script.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,36 @@ students = transform(students, weight = as.numeric(as.character(weight)))

students$name = c("Maria", "Franz", "Peter", "Lisa", "Hans", "Eva", "Mia", "Karl")



checkHeight3 = function(students.input = students){

result.frame = data.frame(matrix(NA, nrow = nrow(students.input), ncol = 2))
colnames(result.frame) = c("name", "difference")

male.mean = students.input %>%
filter(sex == "M") %>%
summarise(mean = mean(height))
female.mean = students.input %>%
filter(sex == "F") %>%
summarise(mean = mean(height))

for (i in 1:nrow(students.input)) {
# calculate sex-specific deviations from the mean
if (students.input[i, "sex"] == "F") {
height.diff = 100*(students.input[i,]$height - female.mean$mean)
}
else {
height.diff = 100*(students.input[i, ]$height - male.mean$mean)
}
result.frame[i, "name"] = as.character(students.input[i, "name"])
result.frame[i, "difference"] = height.diff
}
# Input:
# Argument1, class of argument1 object

#' calculate sex specific height difference of persons in a data.frame to
#' the average height
#' @param students.input ('data.frame') \cr
#' data.frame with columns height, sex and name
#' @return ('data.frame') \cr
#' returns data.frame with names and difference to average gender height
checkHeight3 <- function(students.input = students){
## average height by gender
women_mean_height = mean(with(students.input, height[sex=="F"]))
men_mean_height = mean(with(students.input, height[sex=="M"]))

## initialize vector to store height difference for every person in df
height_vector = c()

## apply function to calculate difference and store the value in height_vector
height_vector = apply(students.input, MARGIN = 1,
FUN = function(student){
#substract the gender specific means from the individuals to get height differnces
(if (student["sex"] == "M") men_mean_height - as.numeric(student["height"])
else women_mean_height - as.numeric(student["height"]) )
} )

## create return data.frame
result.frame = data.frame("name" = students.input$name, "sexspec_height_diff" = height_vector*100)

## return resulting data.frame
return(result.frame)
}

checkHeight3(students.input = students)
print(checkHeight3(students.input = students))