包含值列表的Dataframe

时间:2013-08-15 09:43:06

标签: r

Attribute             Time       Value
pmEulRlcUserPacketThp 2013-04-30 12,51,34,17 
pmEulRlcUserPacketThp 2013-04-30 84,28,17,10 
pmEulRlcUserPacketThp 2013-04-30 11,43,28,15
pmEulRlcUserPacketThp 2013-04-30 80,26,17,91 
pmEulRlcUserPacketThp 2013-04-30 10,41,25,13 
pmEulRlcUserPacketThp 2013-04-30 97,35,23,12

我正在公司实习!他们有这样的数据进行KS测试。值列是一个值数组,但R将其读作字符。我希望在AttributepmEulRlcUserPacketThpTime2013-04-30时计算所有值的总和。我怎样才能做到这一点?

Attribute包含各种pm ...... Time是从30-4-201330-5-2013的每月数据。所以我应该为每个Attribute和每个日期都有一个向量。 请帮助我..!

它不适用于行中不同的矢量长度

df = read.table(text="Attribute             Time       Value
    pmEulRlcUserPacketThp 2013-04-30 12,51,34,17 
    pmEulRlcUserPacketThp 2013-04-30 84,28,17,10 
    pmEulRlcUserPacketThp 2013-04-30 11,43,28,15
    pmEulRlcUserPacketThp 2013-04-30 80,26,17,91 
    pmEulRlcUserPacketThp 2013-04-30 10,41,25,13 
    pmEulRlcUserPacketThp 2013-04-30 97,35,23,12,13", 
                 header = TRUE, fill = TRUE, stringsAsFactors=F)
dfL <- concat.split.multiple(df, "Value", direction = "long")

"Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 6, 7" 

这是我得到的错误! 包含不同长度的矢量的数据可以做什么?

对于不同的日期:

df = read.table(text="Attribute Time Value
 pmEulRlcUserPacketThp 2013-04-30 12,51,34,17
 pmEulRlcUserPacketThp 2013-04-29 84,28,17,10
 pmEulRlcUserPacketThp 2013-04-28 11,43,28,15
 pmEulRlcUserPacketThp 2013-04-27 80,26,17,91
 pmEulRlcUserPacketThp 2013-04-26 10,41,25,13
 pmEulRlcUserPacketThp 2013-04-25 97,35,23,12",
                 header = TRUE, fill = TRUE, stringsAsFactors=F) 

现在我的数据看起来像这样。我已完成所有连接步骤 - 我现在拥有的数据是

> y
              Attribute       Time V1 V2 V3 V4
1 pmEulRlcUserPacketThp 2013-04-30 12 51 34 17
2 pmEulRlcUserPacketThp 2013-04-29 84 28 17 10
3 pmEulRlcUserPacketThp 2013-04-28 11 43 28 15
4 pmEulRlcUserPacketThp 2013-04-27 80 26 17 91
5 pmEulRlcUserPacketThp 2013-04-26 10 41 25 13
6 pmEulRlcUserPacketThp 2013-04-25 97 35 23 12

现在我想要的是,V1,V2,V3,V4聚合两个时间段 - 一个是(第27到第30个,另一个是第25到第26个)。我正在使用子集,这对于许多元素的大量数据是不可行的。

> y1<-y[1:4,]
> y1
              Attribute       Time V1 V2 V3 V4
1 pmEulRlcUserPacketThp 2013-04-30 12 51 34 17
2 pmEulRlcUserPacketThp 2013-04-29 84 28 17 10
3 pmEulRlcUserPacketThp 2013-04-28 11 43 28 15
4 pmEulRlcUserPacketThp 2013-04-27 80 26 17 91

> y2<-y[-(1:4),]
> y2
              Attribute       Time V1 V2 V3 V4
5 pmEulRlcUserPacketThp 2013-04-26 10 41 25 13
6 pmEulRlcUserPacketThp 2013-04-25 97 35 23 12

> z1<-aggregate(V1 ~ Attribute, y1, sum)
> z1
              Attribute  V1
1 pmEulRlcUserPacketThp 187
> z2<-aggregate(V1 ~ Attribute, y2, sum)
> z2
              Attribute  V1
1 pmEulRlcUserPacketThp 107

对于两个不同的时间间隔,这是V1的jus。这也必须为其他值(V2,V3,V4)完成。这很费时间。有没有办法使用聚合来选择日期?

2 个答案:

答案 0 :(得分:3)

自从最初被问到这个问题以来,你的问题确实发生了很大变化,而这通常不是很好的SO行为。但是,我感觉很慷慨......

此解决方案使用来自“splitstackshape”的concat.split.multiple和来自基准R的aggregatecut来获取您想要的解决方案:

加载“splitstackshape”并确保它至少是版本1.2.0(在发布此内容时最近):

library(splitstackshape)
## Make sure you're running at least version 1.2.0
packageVersion("splitstackshape")
# [1] ‘1.2.0’

以下是您的示例数据:

df <- read.table(text="Attribute Time Value
 pmEulRlcUserPacketThp 2013-04-30 12,51,34,17
 pmEulRlcUserPacketThp 2013-04-29 84,28,17,10
 pmEulRlcUserPacketThp 2013-04-28 11,43,28,15
 pmEulRlcUserPacketThp 2013-04-27 80,26,17,91
 pmEulRlcUserPacketThp 2013-04-26 10,41,25,13
 pmEulRlcUserPacketThp 2013-04-25 97,35,23,12",
 header = TRUE, fill = TRUE, stringsAsFactors = FALSE)

首先,拆分“价值”栏。

y <- concat.split.multiple(df, "Value")

接下来,为您要使用的日期范围创建“间隔”列。

y$interval <- cut(as.Date(y$Time), breaks=c(as.Date(
  c("2013-04-25", "2013-04-27", "2013-04-30"))), include.lowest=TRUE)

最后,aggregate您的数据。 . ~表示法允许您一次聚合所有非ID列。

aggregate(. ~ Attribute + interval, y[-2], sum)
#               Attribute   interval Value_1 Value_2 Value_3 Value_4
# 1 pmEulRlcUserPacketThp 2013-04-25     107      76      48      25
# 2 pmEulRlcUserPacketThp 2013-04-27     187     148      96     133

仅供参考:如果您使用的是1.2.0版本,则不应再收到帖子中提到的错误。该错误是由于read.table如何决定要创建的列数。它只读取前5行,而遇到问题的示例在第6行有较长的行。我已经实现count.fields来克服这个问题。感谢您引起我的注意。

答案 1 :(得分:0)

这是否接近你想要的?

df = read.table(text="Attribute             Time       Value
  pmEulRlcUserPacketThp 2013-04-30 12,51,34,17 
  pmEulRlcUserPacketThp 2013-04-30 84,28,17,10 
  pmEulRlcUserPacketThp 2013-04-30 11,43,28,15
  pmEulRlcUserPacketThp 2013-04-30 80,26,17,91 
  pmEulRlcUserPacketThp 2013-04-30 10,41,25,13 
  pmEulRlcUserPacketThp 2013-04-30 97,35,23,12", 
                header = TRUE, fill = TRUE, stringsAsFactors=F)


values = data.frame(t(matrix(unlist(strsplit(df$Value, ',')), ncol = nrow(df))))
values = mapply(values, FUN = function(row){as.numeric(as.character(row))})
df = cbind(df[,1:2], values)

aggregate(df$X1, by=list(df$Attribute, df$Time), FUN=sum)
aggregate(df$X2, by=list(df$Attribute, df$Time), FUN=sum)