prop.table 有多个变量
prop.table with multiple variable
我想重新创建一个由 SurveyMonkey 填充的 table(示例:http://imgur.com/kt3d00g)- 左侧是问题(例如,因素 X 有多重要?)然后离散变量总是相同的:一点也不,稍微......等等
获取格式数据的最佳方法是什么(#s 列是问题并以结果 prop.table 结尾?:
Q1 Q2 Q3 Q4
Very Very Very Moderately
Moderately Slightly Extremely Moderately
Not at all Very Very Slightly
Not at all Moderately Not at all Not at all
Extremely Extremely Extremely Extremely
Slightly Very Extremely Slightly
Tidy your data先。假设您的原始数据集位于 data.frame
中,名为 df
:
library(tidyr)
df.tidy <- gather(df, question, result)
prop.table(table(df.tidy$question, df.tidy$result))
# Extremely Moderately Not at all Slightly Very
# Q1 0.04166667 0.04166667 0.08333333 0.04166667 0.04166667
# Q2 0.04166667 0.04166667 0.00000000 0.04166667 0.12500000
# Q3 0.12500000 0.00000000 0.04166667 0.00000000 0.08333333
# Q4 0.04166667 0.08333333 0.04166667 0.08333333 0.00000000
# Additional stuff to look at to check your understanding...
#
# df.tidy
# table(df.tidy$question, df.tidy$result)
# prop.table(table(df.tidy$question, df.tidy$result), margin = 1)
# prop.table(table(df.tidy$question, df.tidy$result), margin = 2)
如果您需要 "by-question" 个百分比,您需要在对 prop.table()
的调用中使用 margin = 1
-- 请参阅 ?prop.table
我想重新创建一个由 SurveyMonkey 填充的 table(示例:http://imgur.com/kt3d00g)- 左侧是问题(例如,因素 X 有多重要?)然后离散变量总是相同的:一点也不,稍微......等等
获取格式数据的最佳方法是什么(#s 列是问题并以结果 prop.table 结尾?:
Q1 Q2 Q3 Q4
Very Very Very Moderately
Moderately Slightly Extremely Moderately
Not at all Very Very Slightly
Not at all Moderately Not at all Not at all
Extremely Extremely Extremely Extremely
Slightly Very Extremely Slightly
Tidy your data先。假设您的原始数据集位于 data.frame
中,名为 df
:
library(tidyr)
df.tidy <- gather(df, question, result)
prop.table(table(df.tidy$question, df.tidy$result))
# Extremely Moderately Not at all Slightly Very
# Q1 0.04166667 0.04166667 0.08333333 0.04166667 0.04166667
# Q2 0.04166667 0.04166667 0.00000000 0.04166667 0.12500000
# Q3 0.12500000 0.00000000 0.04166667 0.00000000 0.08333333
# Q4 0.04166667 0.08333333 0.04166667 0.08333333 0.00000000
# Additional stuff to look at to check your understanding...
#
# df.tidy
# table(df.tidy$question, df.tidy$result)
# prop.table(table(df.tidy$question, df.tidy$result), margin = 1)
# prop.table(table(df.tidy$question, df.tidy$result), margin = 2)
如果您需要 "by-question" 个百分比,您需要在对 prop.table()
的调用中使用 margin = 1
-- 请参阅 ?prop.table