合并两个变量以创建频率表

发布于 2024-09-27 05:57:06 字数 840 浏览 6 评论 0原文

我还有一个新手问题;

假设我有一组数字

graph_val <- c(4,2,3,4,1,1,9)

,我需要根据这个比例创建它们的频率表

           1            2            3            4            5            9 
 "Very Poor"       "Poor"    "Average"       "Good"  "Very Good" "Don't Know" 

本质上我想知道的是如何将表格转换为这种格式:

 "Very Poor"       "Poor"    "Average"       "Good"  "Very Good" "Don't Know"
           2            1            1            1            0            1 

或者至少:

           1            2            3            4            5            9
           2            1            1            1            0            1 

我可以添加标签稍后将 name.arg 与 barplot 2 一起使用。

我已经在这一天的大部分时间里都在做这件事,之后我的自动化工作的其余部分就一帆风顺了。我认为我在制表方面走在正确的轨道上,但无法完全实现目标。

I have another newbie question;

lets say I have a set of numbers

graph_val <- c(4,2,3,4,1,1,9)

and I need to create a frequency table of them against this scale

           1            2            3            4            5            9 
 "Very Poor"       "Poor"    "Average"       "Good"  "Very Good" "Don't Know" 

Essentially what I want to know is how do I get a table into this format:

 "Very Poor"       "Poor"    "Average"       "Good"  "Very Good" "Don't Know"
           2            1            1            1            0            1 

or at the very least:

           1            2            3            4            5            9
           2            1            1            1            0            1 

And I can add the labels in later using names.arg with barplot 2.

I've been on this for most of the day, after this its clear sailing for the rest of my automation job. I thought I was on the right track with tabulate but couldn't quite get there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山田美奈子 2024-10-04 05:57:06

首先,您需要考虑数据。完全按照考虑分类变量的方式来考虑因素。级别告诉它会发生什么,标签给它一个漂亮的名字。

graph_val <- factor(graph_val, levels=c(1,2,3,4,5,9), labels=strsplit('
Very Poor
Poor
Average
Good
Very Good
Don\'t Know
', '\n')[[1]][-1]) 
## Take note of the escape character in Don\'t Know

summary(graph_val)

如果你需要百分比,你可以这样做:

summary(graph_val)/length(graph_val)\

或者这样:

round(summary(graph_val)/length(graph_val),2)

First you need to factor your data. Think of a factor exactly in the way that you would think of a categorical variable. Levels tells it what to expect, labels gives it a pretty name.

graph_val <- factor(graph_val, levels=c(1,2,3,4,5,9), labels=strsplit('
Very Poor
Poor
Average
Good
Very Good
Don\'t Know
', '\n')[[1]][-1]) 
## Take note of the escape character in Don\'t Know

summary(graph_val)

If you need percentages, you can do something like this:

summary(graph_val)/length(graph_val)\

Or this:

round(summary(graph_val)/length(graph_val),2)
韬韬不绝 2024-10-04 05:57:06

“R 简介”中的以下内容直接回答了您的问题:

http://cran.r-project.org/doc/manuals/R-intro.html#Frequency-tables-from-factors

The following from "An Introduction to R" directly answers your question:

http://cran.r-project.org/doc/manuals/R-intro.html#Frequency-tables-from-factors

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文