Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.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_Dataframe_Dplyr - Fatal编程技术网

R 如何将因子类型的多个列中不为空的元素总数相加?

R 如何将因子类型的多个列中不为空的元素总数相加?,r,dataframe,dplyr,R,Dataframe,Dplyr,提供了这样一个数据帧: df <- data.frame(list(Group = c("Group1", "Group1", "Group2", "Group2"), A=c("Some text", "Text here too", "Some other text", NA), B=c(NA, "Some random text", NA, "Random here too"))) > d

提供了这样一个数据帧:

df <- data.frame(list(Group = c("Group1", "Group1", "Group2", "Group2"),
                      A=c("Some text", "Text here too", "Some other text", NA), 
                      B=c(NA, "Some random text", NA, "Random here too")))
> df
   Group               A                B
1 Group1       Some text             <NA>
2 Group1   Text here too Some random text
3 Group2 Some other text             <NA>
4 Group2            <NA>  Random here too
虽然这是一个愚蠢的数据帧示例(原始数据帧有更多的列和组,手动实现结果并不容易),但由于我无法使用因子操作,所以我没有成功。此外,我担心我的方法(见下文)过于冗长,可能有些过激,这使得它不太适合我的实际数据帧,因为它包含更多的列

这就是我到目前为止所做的:

# Manually create a new numeric column with numbers.
df$A_n = as.character(df$A)
df$A_n[!is.na(df$A_n)] <- 1
df$A_n = as.numeric(df$A_n)

df$B_n = as.character(df$B)
df$B_n[!is.na(df$B_n)] <- 1
df$B_n = as.numeric(df$B_n)
但是,我得到了意外的数据帧:

> df.expected
   Group A_n B_n
1 Group1   2   1
2 Group2   1   1
> df2
# A tibble: 2 x 3
   Group   A_n   B_n
  <fctr> <dbl> <dbl>
1 Group1     2    NA
2 Group2    NA    NA
>df2
#一个tibble:2x3
A组B组
1组1 2 NA
2组2 NA NA
有谁能帮助我以更好的方式解决这个问题和/或告诉我dplyr的代码块有什么问题吗?

我的dplyr代码块有什么问题吗? 这是因为有
NA
s。试一试

library(dplyr)  

df2 = df %>% 
      select(Group, A_n, B_n) %>% 
      group_by(Group) %>% 
      summarise_all(sum, na.rm=TRUE)
相反

我的机器上的输出:

# A tibble: 2 x 3
   Group   A_n   B_n
  <fctr> <dbl> <dbl>
1 Group1     2     1
2 Group2     1     1
# A tibble: 2 x 3
   Group     A     B
  <fctr> <int> <int>
1 Group1     2     1
2 Group2     1     1
#一个tible:2x3
A组B组
1组1 2 1
2组2 1 1
恐怕我的方法。。。太啰嗦了,可能太过分了 您可以这样做:

df <- data.frame(list(Group = c("Group1", "Group1", "Group2", "Group2"),
                      A=c("Some text", "Text here too", "Some other text", NA), 
                      B=c(NA, "Some random text", NA, "Random here too")))

library(dplyr)

df2 = df %>% 
    group_by(Group) %>% 
    summarise_all(.funs=function(x) length(na.omit(x)))
df%
分组依据(分组)%>%
总结所有内容(.funs=函数(x)长度(na.省略(x)))
我的机器上的输出:

# A tibble: 2 x 3
   Group   A_n   B_n
  <fctr> <dbl> <dbl>
1 Group1     2     1
2 Group2     1     1
# A tibble: 2 x 3
   Group     A     B
  <fctr> <int> <int>
1 Group1     2     1
2 Group2     1     1
#一个tible:2x3
A、B组
1组1 2 1
2组2 1 1
一点解释 如果您查看
帮助(总结所有内容)
,您将看到它的参数是
.tbl
.funs
、和
(我们现在不担心省略号)。因此,我们使用管道
%%>%%
df
输入到
groupby()
中,然后再次使用管道
%%>%%
将其输入到
summary\u all()
。这将处理
.tbl
参数。
.funs
参数用于指定应使用哪些函数汇总到
.tbl
中的所有非分组列。在这里,我们想知道每个列中有多少元素不是
NA
,我们可以通过将
长度(NA.omit(x))
应用到
.tbl
中的每个非分组列
x
来实现(作为一种方法)

对于学习
dplyr
的资源,我的最佳建议是,一本由谁编写的
dplyr
包(以及许多其他包)的书。

我对dplyr的代码块有什么错? 这是因为有
NA
s。试一试

library(dplyr)  

df2 = df %>% 
      select(Group, A_n, B_n) %>% 
      group_by(Group) %>% 
      summarise_all(sum, na.rm=TRUE)
相反

我的机器上的输出:

# A tibble: 2 x 3
   Group   A_n   B_n
  <fctr> <dbl> <dbl>
1 Group1     2     1
2 Group2     1     1
# A tibble: 2 x 3
   Group     A     B
  <fctr> <int> <int>
1 Group1     2     1
2 Group2     1     1
#一个tible:2x3
A组B组
1组1 2 1
2组2 1 1
恐怕我的方法。。。太啰嗦了,可能太过分了 您可以这样做:

df <- data.frame(list(Group = c("Group1", "Group1", "Group2", "Group2"),
                      A=c("Some text", "Text here too", "Some other text", NA), 
                      B=c(NA, "Some random text", NA, "Random here too")))

library(dplyr)

df2 = df %>% 
    group_by(Group) %>% 
    summarise_all(.funs=function(x) length(na.omit(x)))
df%
分组依据(分组)%>%
总结所有内容(.funs=函数(x)长度(na.省略(x)))
我的机器上的输出:

# A tibble: 2 x 3
   Group   A_n   B_n
  <fctr> <dbl> <dbl>
1 Group1     2     1
2 Group2     1     1
# A tibble: 2 x 3
   Group     A     B
  <fctr> <int> <int>
1 Group1     2     1
2 Group2     1     1
#一个tible:2x3
A、B组
1组1 2 1
2组2 1 1
一点解释 如果您查看
帮助(总结所有内容)
,您将看到它的参数是
.tbl
.funs
、和
(我们现在不担心省略号)。因此,我们使用管道
%%>%%
df
输入到
groupby()
中,然后再次使用管道
%%>%%
将其输入到
summary\u all()
。这将处理
.tbl
参数。
.funs
参数用于指定应使用哪些函数汇总到
.tbl
中的所有非分组列。在这里,我们想知道每个列中有多少元素不是
NA
,我们可以通过将
长度(NA.omit(x))
应用到
.tbl
中的每个非分组列
x
来实现(作为一种方法)


对于要了解
dplyr
的资源,我的最佳建议是编写
dplyr
包(以及许多其他包)的作者的一本书。

在base R中,您可以将
aggregate
与标准接口(与公式接口相反)一起使用


cbind
要计算的变量,并提供这些变量的名称。在第二个参数中,包括分组变量。然后,在运行时,对未缺失元素的na指示符求和。

在基数R中,可以使用标准接口(与公式接口相反)的
聚合


cbind
要计算的变量,并提供这些变量的名称。在第二个参数中,包括分组变量。然后,在运行过程中,对未缺失元素的na指示器求和。

谢谢!它的工作原理很有魅力,但老实说,我不明白你在总结什么,因为我对这个点一点都不熟悉,也不熟悉有趣的东西。。。你能在我能读到/了解的地方给我写信吗?当然可以。我将在一小时内编辑我的答案,包括附加的解释和链接。非常感谢您提供进一步的解释!非常感谢。它的工作原理很有魅力,但老实说,我不明白你在总结什么,因为我对这个点一点都不熟悉,也不熟悉有趣的东西。。。你能在我能读到/了解的地方给我写信吗?当然可以。我将在一小时内编辑我的答案,包括附加的解释和链接。非常感谢您提供进一步的解释!谢谢你花时间回答这个问题。我更喜欢另一个答案,因为我更熟悉dplyr语法,因为它需要键入更少的列名称,这是一个额外的点,因为我有很多列。谢谢你花时间回答这个问题。我更喜欢另一个答案,因为我更熟悉dplyr语法,因为它需要键入更少的列名称,这是一个额外的要点,因为我有很多列。