Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.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 tible数据类型的行求和_R_Dplyr - Fatal编程技术网

R tible数据类型的行求和

R tible数据类型的行求和,r,dplyr,R,Dplyr,我有一个Tibble,我注意到dplyr::rowwise()和sum()的组合不起作用。我知道在这个主题上有很多线索,我有2到3个解决方案,但我不太明白为什么rowwise()和sum()组合不起作用 因此,我的问题是:rowwise()和sum()的组合为什么不起作用?我们该怎么做才能让它起作用?我是一个初学者,所以我相信我在下面的代码中做了一些错误的事情 数据: dput(data) structure(list(Fiscal.Year = c(2016L, 2016L, 2016L, 2

我有一个Tibble,我注意到
dplyr::rowwise()
sum()
的组合不起作用。我知道在这个主题上有很多线索,我有2到3个解决方案,但我不太明白为什么
rowwise()
sum()
组合不起作用

因此,我的问题是:
rowwise()
sum()
的组合为什么不起作用?我们该怎么做才能让它起作用?我是一个初学者,所以我相信我在下面的代码中做了一些错误的事情

数据:

dput(data)
structure(list(Fiscal.Year = c(2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L), col1 = c(0, 26613797.764311, 
0, 12717073.587292, 0, 0, 0, 0, 0, 0), col2 = c(0, 0, 0, 0, 8969417.89721166, 
0, 11483606.8417117, 0, 0, 0), col3 = c(0, 0, 33251606.347943, 
0, 25082683.4492186, 0, 17337191.3014127, 0, 0, 0), col4 = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), col5 = c(0, 0, 0, 0, 0, 0, 0, 0, 
0, 9796823.229998), col6 = c(35822181.695755, 17475066.870565, 
0, 0, 0, 0, 4040695.327278, 0, 13117249.623068, 0), col7 = c(0, 
0, 0, 0, 0, 18347258.910001, 0, 0, 7002205.087399, 0), No.Trans = c(2987L, 
1292L, 1002L, 796L, 691L, 677L, 400L, 388L, 381L, 366L)), .Names = c("Fiscal.Year", 
"col1", "col2", "col3", "col4", "col5", "col6", "col7", "No.Trans"
), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
data %>%  #No
        dplyr::rowwise() %>%
        dplyr::mutate(sum = sum(.[2:8]))
  rowSums(data[,2:8])
data %>%
        select(2:8)%>%
        dplyr::mutate(sum=rowSums(.))
此代码不起作用:

dput(data)
structure(list(Fiscal.Year = c(2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L), col1 = c(0, 26613797.764311, 
0, 12717073.587292, 0, 0, 0, 0, 0, 0), col2 = c(0, 0, 0, 0, 8969417.89721166, 
0, 11483606.8417117, 0, 0, 0), col3 = c(0, 0, 33251606.347943, 
0, 25082683.4492186, 0, 17337191.3014127, 0, 0, 0), col4 = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), col5 = c(0, 0, 0, 0, 0, 0, 0, 0, 
0, 9796823.229998), col6 = c(35822181.695755, 17475066.870565, 
0, 0, 0, 0, 4040695.327278, 0, 13117249.623068, 0), col7 = c(0, 
0, 0, 0, 0, 18347258.910001, 0, 0, 7002205.087399, 0), No.Trans = c(2987L, 
1292L, 1002L, 796L, 691L, 677L, 400L, 388L, 381L, 366L)), .Names = c("Fiscal.Year", 
"col1", "col2", "col3", "col4", "col5", "col6", "col7", "No.Trans"
), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
data %>%  #No
        dplyr::rowwise() %>%
        dplyr::mutate(sum = sum(.[2:8]))
  rowSums(data[,2:8])
data %>%
        select(2:8)%>%
        dplyr::mutate(sum=rowSums(.))

仅供参考,我已经尝试了下面的代码集,它们很有效。我特别想寻找一种使用
rowwise()
sum()
的解决方案

选项1: 讨论地点:

选项2:

dput(data)
structure(list(Fiscal.Year = c(2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L), col1 = c(0, 26613797.764311, 
0, 12717073.587292, 0, 0, 0, 0, 0, 0), col2 = c(0, 0, 0, 0, 8969417.89721166, 
0, 11483606.8417117, 0, 0, 0), col3 = c(0, 0, 33251606.347943, 
0, 25082683.4492186, 0, 17337191.3014127, 0, 0, 0), col4 = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), col5 = c(0, 0, 0, 0, 0, 0, 0, 0, 
0, 9796823.229998), col6 = c(35822181.695755, 17475066.870565, 
0, 0, 0, 0, 4040695.327278, 0, 13117249.623068, 0), col7 = c(0, 
0, 0, 0, 0, 18347258.910001, 0, 0, 7002205.087399, 0), No.Trans = c(2987L, 
1292L, 1002L, 796L, 691L, 677L, 400L, 388L, 381L, 366L)), .Names = c("Fiscal.Year", 
"col1", "col2", "col3", "col4", "col5", "col6", "col7", "No.Trans"
), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
data %>%  #No
        dplyr::rowwise() %>%
        dplyr::mutate(sum = sum(.[2:8]))
  rowSums(data[,2:8])
data %>%
        select(2:8)%>%
        dplyr::mutate(sum=rowSums(.))
选项3: 讨论地点:

选项4:

dput(data)
structure(list(Fiscal.Year = c(2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L), col1 = c(0, 26613797.764311, 
0, 12717073.587292, 0, 0, 0, 0, 0, 0), col2 = c(0, 0, 0, 0, 8969417.89721166, 
0, 11483606.8417117, 0, 0, 0), col3 = c(0, 0, 33251606.347943, 
0, 25082683.4492186, 0, 17337191.3014127, 0, 0, 0), col4 = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), col5 = c(0, 0, 0, 0, 0, 0, 0, 0, 
0, 9796823.229998), col6 = c(35822181.695755, 17475066.870565, 
0, 0, 0, 0, 4040695.327278, 0, 13117249.623068, 0), col7 = c(0, 
0, 0, 0, 0, 18347258.910001, 0, 0, 7002205.087399, 0), No.Trans = c(2987L, 
1292L, 1002L, 796L, 691L, 677L, 400L, 388L, 381L, 366L)), .Names = c("Fiscal.Year", 
"col1", "col2", "col3", "col4", "col5", "col6", "col7", "No.Trans"
), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
data %>%  #No
        dplyr::rowwise() %>%
        dplyr::mutate(sum = sum(.[2:8]))
  rowSums(data[,2:8])
data %>%
        select(2:8)%>%
        dplyr::mutate(sum=rowSums(.))

这些列看起来很像观察结果……
如果是这样的话,整理数据帧将大大简化数据争用

这能让你找到你想要的答案吗

data %>%
    gather(key = col, val = revenue, `col1`:`col7`) %>%
    group_by(Fiscal.Year, No.Trans) %>%
    summarise(res = sum(revenue))

Source: local data frame [10 x 3]
Groups: Fiscal.Year [?]

   Fiscal.Year No.Trans      res
         <int>    <int>    <dbl>
1         2016      366  9796823
2         2016      381 20119455
3         2016      388        0
4         2016      400 32861493
5         2016      677 18347259
6         2016      691 34052101
7         2016      796 12717074
8         2016     1002 33251606
9         2016     1292 44088865
10        2016     2987 35822182
数据%>%
聚集(key=col,val=revenue,`col1`:`col7`)%>%
集团单位(会计年度,编号)%>%
总结(res=总额(收入))
来源:本地数据帧[10 x 3]
分组:财政年度[?]
财政年度编号Trans res
1         2016      366  9796823
2         2016      381 20119455
3         2016      388        0
4         2016      400 32861493
5         2016      677 18347259
6         2016      691 34052101
7         2016      796 12717074
8         2016     1002 33251606
9         2016     1292 44088865
10        2016     2987 35822182
想要真正流畅地介绍整洁思考,请尝试。他在演示文稿中讨论的功能已经更新,但哈德利在教授这门学科方面做得很好:通过教学链接


更新的函数可以在他的ggplot2手册中找到

为什么你需要走这条路线,与你在文章中提到的一些方法相比,这条路线效率不高post@akrun-谢谢你的提问。“按效率”,你指的是R基地的那个吗?作为一个初学者,我相信我缺乏关于dplyr的知识。因此,我想学习如何修复错误。这对我很有帮助。
rowSums
Reduce
这两种方法非常有效。我不确定它为什么不起作用。您得到的值与
数据[-1]]>%rowwise()%%>%sum相同,即:整个值的总和,可能是错误或设计功能