【问题标题】:Create count tables without mentioning variable names in a data table within r创建计数表而不在 r 中的数据表中提及变量名称
【发布时间】:2021-04-11 21:31:20
【问题描述】:

有没有一种方法可以为 R 中的数据表中的所有变量创建计数/频率表,而无需明确提及表中的所有变量?

我的数据表如下所示:

我需要为表中的所有变量创建计数表,而不提及数据表中的变量名称。

例如烟雾频率表可能如下所示:

有什么建议吗?

【问题讨论】:

  • 试试lapply(your_data[, -1], table)(未测试)
  • 你能提供数据而不是使用dput(data)的图像
  • 谢谢马库斯!与 View 一起使用时,它确实包含计数。

标签: r dataframe frequency


【解决方案1】:

同意@markus。只是想添加以下内容-

try <- data.frame(id= c(1,2,3,4,5),
                    smoke= c(1,0,1,1,0),
                    run= c(1,0,0,1,1),
                    eat= c(0,1,1,1,1),
                    sleep = c(1,0,0,1,1))
X <- lapply(try[,-1], table) #As suggested by markus


func1 <- function(x){
  y <- as.data.frame(x)
  colnames(y) <- c("Values", "Frequency")
  y
}
func1(X$sleep)

输出 -

> func1(X$sleep)
  Values Frequency
1      0         2
2      1         3

谢谢!

【讨论】:

  • 感谢 Girish 提供详细代码。 Markus 的代码运行良好。唯一要做的就是查看(X)。
【解决方案2】:
try <- data.frame(id= c(1,2,3,4,5),
                smoke= c(1,0,1,1,0),
                run= c(1,0,0,1,1),
                eat= c(0,1,1,1,1),
                sleep = c(1,0,0,1,1)) #from Girish

X <- lapply(try[,-1], table) #As suggested by markus

View(X)

【讨论】:

    猜你喜欢
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 2020-07-21
    • 2021-02-17
    • 1970-01-01
    相关资源
    最近更新 更多