将具有多个因子的(因子)数据分组.错误:大小不兼容(0),预期为1(组大小)或1 [英] Group (factorial) data with multiple factors. error: incompatible size (0), expecting 1 (the group size) or 1

查看:120
本文介绍了将具有多个因子的(因子)数据分组.错误:大小不兼容(0),预期为1(组大小)或1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此帖子是

This post is a following up of Changing line color in ggplot based on "several factors" slope

我想按"PQ"对数据(波纹管)进行分组,但是出现以下错误:

I would like to group the data (bellow) by "PQ", however I get the following error:

大小不兼容(0),期望1(组大小)或1"

"incompatible size (0), expecting 1 (the group size) or 1"

数据

ID<-c("A_P1","A_P1","A_P1","A_P1","A_P1","A_P2","A_P2","A_P2","A_P2","A_P2","A_P2","B_P1","B_P1","B_P1","B_P1","B_P1","B_P1","B_P1","B_P1","B_P2","B_P2","B_P2","B_P2","B_P2","B_P2","B_P2","B_P2")
Q<-c("C1","C1","C2","C3","C3","C1","C1","C2","C2","C3","C3","Q1","Q1","Q1","Q1","Q3","Q3","Q4","Q4","Q1","Q1","Q1","Q1","Q3","Q3","Q4","Q4")
PQ<-c("A_P1C1","A_P1C1","A_P1C2","A_P1C3","A_P1C3","A_P2C1","A_P2C1","A_P2C2","A_P2C2","A_P2C3","A_P2C3","B_P1Q1","B_P1Q1","B_P1Q1","B_P1Q1","B_P1Q3","B_P1Q3","B_P1Q4","B_P1Q4","B_P2Q1","B_P2Q1","B_P2Q1","B_P2Q1","B_P2Q3","B_P2Q3","B_P2Q4","B_P2Q4")
AS<-c("CF","CF","CF","CF","CF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF","CTF")
N<-c("N2","N3","N3","N2","N3","N2","N3","N2","N3","N2","N3","N0","N1","N2","N3","N1","N3","N0","N1","N0","N1","N2","N3","N1","N3","N0","N1")
Value<-c(4.7,8.61,8.34,5.89,8.36,1.76,2.4,5.01,2.12,1.88,3.01,2.4,7.28,4.34,5.39,11.61,10.14,3.02,9.45,8.8,7.4,6.93,8.44,7.37,7.81,6.74,8.5)

df<-data.frame(ID=ID,Q=Q,PQ=PQ,AS=AS,N=N,Value=Value)

传递错误的代码

#calculate slopes for N0 and N1
    df %>% 
      filter(N=="N0" | N=="N1") %>%
      group_by(PQ) %>%
      # use diff to calculate slope
      mutate(slope = diff(Value)) -> dat01

#calculate slopes for N0 and N2
    df %>% 
      filter(N=="N0" | N=="N2") %>%
      group_by(PQ) %>%
      # use diff to calculate slope
      mutate(slope = diff(Value)) -> dat02

此外,我想计算其余"PQ"因子(如果存在)的斜率,即N0-N3; N1-N2 ...等等

Additionally, I would like to calculate the slope of the the remaining "PQ" factors (when existent), i.e. N0-N3;N1-N2 ... and so on

推荐答案

该错误是由于diff的输出相对于原始数据集的长度不同所致.它返回的元素比原始数据少一个.因此,添加0或NA即可解决问题

The error is due to the difference in length from the output of diff with respect to the original dataset. It returns one element less than the original data. So appending a 0 or NA will solve the issue

df %>% 
   filter(N=="N0" | N=="N1") %>%
   group_by(PQ) %>% 
   mutate(slope = c(0, diff(Value)))

为使结构紧凑,我们可以在有多个元素的情况下使用%in%代替==

To make it compact, instead of ==, we can use %in% when there are multiple elements

df %>%
   filter(N %in%  paste0("N", 0:1)) %>%
   group_by(PQ) %>%
   mutate(slope = c(0, diff(Value)))


关于第二个问题,关于对"N"中的所有组合执行此操作,请在组合后基于"N"的unique元素使用combn,根据组合值使用filter"N"按"PQ"分组,计算值"的diff.输出将是我们指定为simplify = FALSElist.


Regarding the second issue, about doing this for all the combinations in 'N', use the combn on the unique elements of 'N', filter the 'N' based on the combination values, after grouping by 'PQ', calculate the diff of 'Value'. The output will be a list as we specified simplify = FALSE.

combn(as.character(unique(df$N)),2, FUN = function(x) df %>% 
            filter(N %in% x) %>% 
            group_by(PQ) %>%
            mutate(slope = c(0, diff(Value))), simplify = FALSE )

这篇关于将具有多个因子的(因子)数据分组.错误:大小不兼容(0),预期为1(组大小)或1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆