Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这似乎是基本的,但我不会明白。我正在尝试为以下数据计算 R 中的频率表
1 2 2 1 3 1
我想在 csv 输出中传输两种频率,其行将是数据 A 列中的所有唯一条目,其列将是数据 B 列中的所有唯一条目,单元格值将是值出现的次数。我已经探索了一些结构,table但我无法以 csv 格式正确输出值。
table
样本数据的输出:
"","1","2" "1",0,1 "2",1,0 "3",1,0
数据:
df <- read.table(text = "1 2 2 1 3 1")
使用 计算频率table:
as.data.frame(如果您的对象是一个矩阵,您可以使用before将其转换为数据框table。)
as.data.frame
tab <- table(df) V2 V1 1 2 1 0 1 2 1 0 3 1 0
使用函数写入数据write.csv:
write.csv
write.csv(tab, "tab.csv")
结果文件: