简体   繁体   English

如何计算R中tibble中一列的值并求和?

[英]how to count and sum of the values of a column in a tibble in R?

For example, I have this tibble:例如,我有这个小标题:

       nationality    Other
[1,]          White     1 ------> I want to add
[2,]          Mexican   0
[3,]          American  0
[4,]          Asian     1 -------> I want to add
[5,]          af        1 -------> I want to add
[6,]          American  0

I want to somehow sum up the values in Other and create it's own tibble:我想以某种方式总结 Other 中的值并创建它自己的 tibble:

       Other
[1,]     3

I tried using sum(), but it gives me我尝试使用 sum(),但它给了我

  Error in FUN(X[[i]], ...) : 
  only defined on a data frame with all numeric variables

In addition to that, tally() gives me this, it counts the number of rows in the column:除此之外,tally() 给了我这个,它计算列中的行数:

      n
1     88

Here is the code:这是代码:

natgroups3 <- ibtclean %>% select(nationality) %>%mutate(Other = ifelse(str_detect(nationality, "af|White|Asian|white|Middle-Eastern"), yes = 1, no = 0)) %>% drop_na() 

Try to use the tidyverse library.尝试使用tidyverse库。 I prepared a sample code that recreates a tibble d with your structure and calculates the target tibble c with the count of rows with the column other equals to 1.我准备了一个示例代码,它使用您的结构重新创建 tibble d并计算目标 tibble c ,其中列other等于 1 的行数。

library(tidyverse)
d <- tribble(~nationality, ~other, 'White', 0, 'Mexican', 1, 'Amrican', 0, 'Asian', 1, 'af', 1)
d
c <- d %>% count(other) %>% filter(other == 1) %>% select('Other' = n)
c

You can also select the column other and calculate its sum with the following code (according to your business need)您也可以选择other列并使用以下代码计算其总和(根据您的业务需要)

library(tidyverse)
d <- tribble(~nationality, ~other, 'White', 0, 'Mexican', 1, 'Amrican', 0, 'Asian', 1, 'af', 1)
d
c <- d %>% select(other) %>% summarise('Other'=sum(other))
c

Both of the code snippets produce the following result两个代码片段都产生以下结果

# A tibble: 5 x 2
  nationality other
  <chr>       <dbl>
1 White           0
2 Mexican         1
3 Amrican         0
4 Asian           1
5 af              1
# A tibble: 1 x 1
  Other
  <dbl>
1     3

I hope these snippets solve your issue我希望这些片段可以解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 R 中添加不同值的列并计数到相同的小标题 - Add column for distinct values and count to same tibble in R R tibble:按 A 列分组,仅保留 B 列和 C 列中的不同值,并在 C 列中汇总值 - R tibble: Group by column A, keep only distinct values in column B and C and sum values in column C 如何在R的间隔中计算“小节的特定变量的值数”? - How to count the 'number of values of a particular variable of a tibble' in an interval in R? 如何对 R 中的 data.frame 或 tibble 列中的 NA 值的总数求和,并按月和年对它们进行分组 - How can I sum the totals of NA values in a data.frame or tibble column in R and group them by Month and Year R将值添加到组中的tibble列 - R add values to tibble column in groups 在 tibble R 中的列表列中获取值 - get values inside a column of lists in tibble R dplyr:如何使用 count() 将列保留在 tibble 中 - dplyr: how to keep a column in a tibble using count() 如何根据 R 中 tibble 中另一列指示的列的值添加列 - How to add a column based on values of columns indicated by another column in a tibble in R 如何在 R 中将列添加到网状小标题 - how to add a column to a netsed tibble in R 如何使用 R 中现有列中前一行的值创建新列 - How to create a new column with values from previous row in exsisting column, in a tibble, in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM