在R中按特定的值获取行

问题描述 投票:0回答:1

我有这样一个数据表,我想按照职业(列'Profissao')进行分类,我的想法是按照工作领域对每列的答案进行平均。

例如:我需要选择一行中的每一个'Aspeto-A'单元格中的'媒体'工作,并对所有回答表格的媒体人进行平均。

数据表截图

enter image description here

r graphics statistics rstudio average
1个回答
1
投票

你的数据的图片不如使用的数据有用。dput(). 由于我不能使用你的数据,我就用你的 iris R所包含的数据集。

data(iris)
str(iris)
# 'data.frame': 150 obs. of  5 variables:
#  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
aggregate(.~Species, iris, mean)
#      Species Sepal.Length Sepal.Width Petal.Length Petal.Width
# 1     setosa        5.006       3.428        1.462       0.246
# 2 versicolor        5.936       2.770        4.260       1.326
# 3  virginica        6.588       2.974        5.552       2.026

0
投票

毕竟这并不难。可能还有更好的方法,但这个方法是可行的。

AspetoA_Selectiom<-data.frame(Profissao=Profissao,aspetoA=aspetoA)

ApetoA_sum <- aggregate(AspetoA_Selectiom$aspetoA, by=list(AspetoA_Selectiom$Profissao), FUN=sum)

AspetoA_length <- aggregate(AspetoA_Selectiom$aspetoA, by=list(AspetoA_Selectiom$Profissao), FUN=length)

AspetoA_AVG<- ApetoA_sum$x AspetoA_length$x

© www.soinside.com 2019 - 2024. All rights reserved.