我正在尝试创建一个函数,该函数描述(在一个类别中(观察的数量、这些类别的比例,以及是否存在任何NA,用于一个类别变量,并为各个输出提供标题/标签。
为了得到观测的数量及其比例,我使用了一个名为gmodels
的软件包中的函数。它有输出的视觉格式,我想保留在我的新函数中,但不幸的是,它被更改了。这就是我尝试过的:
我将使用以下示例变量值来回答这个问题:
gender <- c("male", "female", "male", "female", "male", "female", NA, "female", "male", "female")
gender <- as.factor(gender)
我想让我的函数运行这个:
CrossTable(gender) #(this is the function from gmodels)
哪个返回这个:
Cell Contents
|-------------------------|
| N |
| N / Table Total |
|-------------------------|
Total Observations in Table: 9
| female | male |
|-----------|-----------|
| 5 | 4 |
| 0.556 | 0.444 |
|-----------|-----------|
然后我想让它运行这个:
sum(is.na(gender))
返回的是:
[1] 1
我创建了我的新功能:
describe_cat <- function(object) {
CrossTable(object)
sum(is.na(object))
}
并获得预期输出
Cell Contents
|-------------------------|
| N |
| N / Table Total |
|-------------------------|
Total Observations in Table: 9
| female | male |
|-----------|-----------|
| 5 | 4 |
| 0.556 | 0.444 |
|-----------|-----------|
[1] 1
我想在每个单独的输出之前包含标题,这样,例如,我就知道[1] 1
指的是什么
describe_cat <- function(object) {
"Observations and Proportions" = CrossTable(object)
"Any NA's?" = sum(is.na(object))
}
但是输出会错过sum()
函数。然后我尝试了这个:
describe_cat <- function(object) {
list(
"Observations and Proportions" = CrossTable(object),
"Any NA's?" = sum(is.na(object))
)
}
这一次,输出更改gmodels格式:
Cell Contents
|-------------------------|
| N |
| N / Table Total |
|-------------------------|
Total Observations in Table: 9
| female | male |
|-----------|-----------|
| 5 | 4 |
| 0.556 | 0.444 |
|-----------|-----------|
$`Observations and Proportions`
$`Observations and Proportions`$t
female male
[1,] 5 4
$`Observations and Proportions`$prop.row
female male
[1,] 0.5555556 0.4444444
$`Observations and Proportions`$prop.col
female male
[1,] 1 1
$`Observations and Proportions`$prop.tbl
female male
[1,] 0.5555556 0.4444444
$`Any NA's?`
[1] 1
我怀疑问题出在list()
函数的某个地方。有什么方法可以获得或多或少类似于此的输出吗?:
$`Observations and Proportions`
Cell Contents
|-------------------------|
| N |
| N / Table Total |
|-------------------------|
Total Observations in Table: 9
| female | male |
|-----------|-----------|
| 5 | 4 |
| 0.556 | 0.444 |
|-----------|-----------|
$`Any NA's?`
[1] 1
CrossTable
返回两个输出,一个显示,另一个返回。
您看到的就是显示的那个。
library(gmodels)
CrossTable(gender)
# Cell Contents
#|-------------------------|
#| N |
#| N / Table Total |
#|-------------------------|
#Total Observations in Table: 9
# | female | male |
# |-----------|-----------|
# | 5 | 4 |
# | 0.556 | 0.444 |
# |-----------|-----------|
它返回的是一个列表,看起来像这样:
temp <- CrossTable(gender)
temp
#$t
# female male
#[1,] 5 4
#$prop.row
# female male
#[1,] 0.5555556 0.4444444
#$prop.col
# female male
#[1,] 1 1
#$prop.tbl
# female male
#[1,] 0.5555556 0.4444444
当您将输出存储在列表中时,它会打印输出并返回列表输出。有其他方法可以计算你在R基中寻找的数字,但如果你想保持CrossTable
函数的格式,你可以使用
describe_cat <- function(object) {
CrossTable(object)
c("Any NA's?" = sum(is.na(object)))
}
describe_cat(gender)
# Cell Contents
#|-------------------------|
#| N |
#| N / Table Total |
#|-------------------------|
#Total Observations in Table: 9
# | female | male |
# |-----------|-----------|
# | 5 | 4 |
# | 0.556 | 0.444 |
# |-----------|-----------|
#Any NA's?
# 1