Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何在R中找到某事物的百分比?_R - Fatal编程技术网

我如何在R中找到某事物的百分比?

我如何在R中找到某事物的百分比?,r,R,我对R非常陌生,这可能是一个非常基本的问题,但假设我有一个包含两列的数据集,其中有由男性和女性组成的学生。一列是学生,另一列是性别。我如何找到每一个的百分比 您可以使用table()函数生成一个表,告诉您学生中有多少男性和女性。然后将此表除以学生总数(您可以使用length()函数得到)。最后把结果乘以100 您的代码应该类似于: proportions <- table(your_data_frame$gender_columnn)/length(your_data_frame$gend

我对R非常陌生,这可能是一个非常基本的问题,但假设我有一个包含两列的数据集,其中有由男性和女性组成的学生。一列是学生,另一列是性别。我如何找到每一个的百分比

您可以使用table()函数生成一个表,告诉您学生中有多少男性和女性。然后将此表除以学生总数(您可以使用length()函数得到)。最后把结果乘以100

您的代码应该类似于:

proportions <- table(your_data_frame$gender_columnn)/length(your_data_frame$gender_column)
percentages <- proportions*100
比例您可以使用table()函数生成一个表,告诉您学生中有多少男性和女性。然后将此表除以学生总数(您可以使用length()函数得到)。最后把结果乘以100

您的代码应该类似于:

proportions <- table(your_data_frame$gender_columnn)/length(your_data_frame$gender_column)
percentages <- proportions*100

比例使用
数据的另一种方法。表

students <- data.frame( names = c( "Bill", "Stacey", "Fred", "Jane", "Sarah" ), 
                        gender = c( "M", "F", "M", "F", "F" ),
                        stringsAsFactors = FALSE )

library( data.table )
setDT( students )[ , 100 * .N / nrow( students ), by = gender ]

#    gender V1
# 1:      M 40
# 2:      F 60

这两个都是用于此类操作的流行软件包,但正如已经指出的,如果您愿意,也可以使用base R。

使用
数据的另一种方法。表

students <- data.frame( names = c( "Bill", "Stacey", "Fred", "Jane", "Sarah" ), 
                        gender = c( "M", "F", "M", "F", "F" ),
                        stringsAsFactors = FALSE )

library( data.table )
setDT( students )[ , 100 * .N / nrow( students ), by = gender ]

#    gender V1
# 1:      M 40
# 2:      F 60

这两个都是类似操作的流行软件包,但是,正如已经指出的,如果您愿意,也可以使用base R。

这可能不是最有效的方法,但这是解决问题的一种方法

首先,您必须创建一个data.frame。人造的是什么

students <- data.frame(student = c("Carla", "Josh", "Amanda","Gabriel", "Shannon", "Tiffany"), gender = c("Female", "Male", "Female", "Male", "Female", "Female")

View(students) 
男女

66.66667 33.33333

想象一下,从百分比的角度来看

其中2 Tablography是我跨列应用sum函数的比例表数据帧(2表示列,1表示行)

因此,如果你只关注少量数据,你可以看到data.frame学生中有2/6=33.3333%的男性,data.frame学生中有4/6=66.66667%的女性,因此我正确地进行了计算

或者

sum(tablature$Female)

[1] 66.66667

sum(tablature$Male)

[1] 33.33333
你可以做一个条形图。当我格式化它时,你必须将它作为一个矩阵来引用,以得到一个条形图

barplot(as.matrix(tablature), xlab = "Gender", main = "Barplot comparison of Gender Among Students", ylab = "Percentages of Student Group")
从这里你可以对性别条形图进行叠加的视觉比较

barplot(as.matrix(tablature), xlab = "Gender", main = "Barplot comparison of Gender Among Students", ylab = "Percentages of Student Group")
这是因为R给每个学生一个16.6667%的盒子

老实说,如果只绘制apply函数的输出,看起来会更好。当然,您可以将其保存到变量中。但是啊哈

barplot(apply(tablature, 2, FUN = sum), col = c("green", "blue"),xlab = "Gender", ylab = "Percentage of Total Students", main = "Barplot showing the Percentages of Gender Represented Among Students", cex.main = 1)
现在它不堆叠了


这可能不是最有效的方法,但这是解决问题的一种方法

首先,您必须创建一个data.frame。人造的是什么

students <- data.frame(student = c("Carla", "Josh", "Amanda","Gabriel", "Shannon", "Tiffany"), gender = c("Female", "Male", "Female", "Male", "Female", "Female")

View(students) 
男女

66.66667 33.33333

想象一下,从百分比的角度来看

其中2 Tablography是我跨列应用sum函数的比例表数据帧(2表示列,1表示行)

因此,如果你只关注少量数据,你可以看到data.frame学生中有2/6=33.3333%的男性,data.frame学生中有4/6=66.66667%的女性,因此我正确地进行了计算

或者

sum(tablature$Female)

[1] 66.66667

sum(tablature$Male)

[1] 33.33333
你可以做一个条形图。当我格式化它时,你必须将它作为一个矩阵来引用,以得到一个条形图

barplot(as.matrix(tablature), xlab = "Gender", main = "Barplot comparison of Gender Among Students", ylab = "Percentages of Student Group")
从这里你可以对性别条形图进行叠加的视觉比较

barplot(as.matrix(tablature), xlab = "Gender", main = "Barplot comparison of Gender Among Students", ylab = "Percentages of Student Group")
这是因为R给每个学生一个16.6667%的盒子

老实说,如果只绘制apply函数的输出,看起来会更好。当然,您可以将其保存到变量中。但是啊哈

barplot(apply(tablature, 2, FUN = sum), col = c("green", "blue"),xlab = "Gender", ylab = "Percentage of Total Students", main = "Barplot showing the Percentages of Gender Represented Among Students", cex.main = 1)
现在它不堆叠了


这个问题已经有了一些很好的答案,但由于最初的提交者承认自己是R的新手,我想提供一个很长的答案。下面的答案所需的步骤超过了所需的最低数量,并且没有使用管道等辅助工具

希望通过这种方式提供答案可以帮助原始提交者了解每一步发生的情况

# Load the dplyr library
library("dplyr")

# Create an example data frame
students <-
  data.frame(
    names = c("Bill", "Stacey", "Fred", "Jane", "Sarah"),
    gender = c("M", "F", "M", "F", "F"),
    stringsAsFactors = FALSE
  )

# Count the total number of students.
total_students <- nrow(students)

# Use dplyr filter to obtain just Female students
all_female_students <- dplyr::filter(students, gender %in% "F")

# Count total number of female students
total_female <- nrow(all_female_students)

# Repeat to find total number of male students
all_male_students <- dplyr::filter(students, gender %in% "M")

total_male <- nrow(all_male_students)

# Divide total female students by total students 
# and multiply result by 100 to obtain a percentage
percent_female <- (total_female / total_students) * 100

# Repeat for males
percent_male <- (total_male / total_students) * 100

> percent_female
[1] 60
> percent_male
[1] 40
#加载dplyr库
图书馆(“dplyr”)
#创建一个示例数据帧

学生们这个问题已经有了一些很好的答案,但是由于最初的提交者承认是R的新手,我想提供一个很长的答案。下面的答案所需的步骤超过了所需的最低数量,并且没有使用管道等辅助工具

希望通过这种方式提供答案可以帮助原始提交者了解每一步发生的情况

# Load the dplyr library
library("dplyr")

# Create an example data frame
students <-
  data.frame(
    names = c("Bill", "Stacey", "Fred", "Jane", "Sarah"),
    gender = c("M", "F", "M", "F", "F"),
    stringsAsFactors = FALSE
  )

# Count the total number of students.
total_students <- nrow(students)

# Use dplyr filter to obtain just Female students
all_female_students <- dplyr::filter(students, gender %in% "F")

# Count total number of female students
total_female <- nrow(all_female_students)

# Repeat to find total number of male students
all_male_students <- dplyr::filter(students, gender %in% "M")

total_male <- nrow(all_male_students)

# Divide total female students by total students 
# and multiply result by 100 to obtain a percentage
percent_female <- (total_female / total_students) * 100

# Repeat for males
percent_male <- (total_male / total_students) * 100

> percent_female
[1] 60
> percent_male
[1] 40
#加载dplyr库
图书馆(“dplyr”)
#创建一个示例数据帧

学生尝试使用
table
prop.table
函数。尝试使用
table
prop.table
函数。
prop.table
有一个参数用于计算组内的比例。例如:
道具表(表(学生),2)
。此外,您在回答中使用的是“引号”而不是代码块-您想要的按钮看起来像“{}”`-或者在每行代码前面加上4个空格。我不知道。一般来说,我认为如果它有效的话并不愚蠢,但是谢谢你告诉我,有一种方法可以通过将更少的函数组合在一起得到正确的答案。
prop.table
有一个参数用于计算组内的比例。例如:
prop.table(table(students),2)
。此外,您在答案中使用的是“引号”而不是代码块-您想要的按钮看起来像“{}”-或者在每行代码前面放4个空格。我不知道。一般来说,我相信如果它能工作的话,这并不愚蠢,但是谢谢你告诉我,有一种方法可以让我们一起找到正确的答案。