Мне было назначено домашнее задание, следующее за
Write a function called numeric_summary which takes two inputs:\
`x`: A numeric vector\
`group`: A factor vector of the same length as x
and produces a list as output which contains the following elements:\
`missing`: The number of missing values in x\
`means`: The means of x for each level of groups.\
`sds`: The standard deviations of x for each level of groups\
`p.value`: The p-value for a test of the hypothesis that the means across the levels of groups are the same (versus the two-sided alternative)\
`is.binary`: Set to FALSE for for this function
Вот мой код до сих пор
numeric_summary <- function(x,group) {
list(missing = sum(is.na(x)),
means = tapply(x, group,mean,na.rm = TRUE),
sds = tapply(x, group, sd,na.rm = TRUE),
p.value = (summary(aov(x ~ group, na.action = na.omit))[[1]][["Pr(>F)"]][1]),
is.binary = apply(titanic4,2,function(x) { all(x %in% 0:1)}))
}
numeric_summary(titanic4$age,titanic4$pclass)
И вывод
$missing
[1] 263
$means
1st 2nd 3rd
39.15992 29.50670 24.81637
$sds
1st 2nd 3rd
14.54806 13.63863 11.95820
$p.value
[1] 1.797203e-43
$is.binary
pclass survived name sex age sibsp parch
FALSE TRUE FALSE FALSE FALSE FALSE FALSE
ticket fare cabin embarked boat body home.dest
FALSE FALSE FALSE FALSE FALSE FALSE FALSE
female
TRUE
Я пытаюсь сделать is.binary прочитанным FALSE
для x, так как это факторная переменная, а не двоичная. Current is.binary считывает каждый столбец во фрейме данных, но я просто хочу, чтобы он читал для выбранного.