【问题标题】:Elegant way of creating subsets from data.frame and binary matrix从 data.frame 和二进制矩阵创建子集的优雅方式
【发布时间】:2013-11-04 11:51:56
【问题描述】:

我有一个包含价格和材料列的数据框,以及一个包含 N 列的真/假矩阵(每列是一种特定类型的材料),T/F 值表示“材料”字符串是否出现在数据矩阵

数据

Price    Material
2.33     Metal nickel linen cotton
3.45     silver emerald steel
7.45     cotton silk wood

矩阵

Metal Nickel Linen Cotton Silver Emerald Steel Cotton Silk Wood
T     T      T     T      0      0       0     0      0    0
0     0      0     0      T      T       T     0      0    0  

...等等。

如何根据材料创建价格子集? 所以我可以计算出材料为“金属”的价格的平均值、范围模式等。

我最初的解决方案是乘以

newMat<- data$price * materialmatrix. 

然后对 newMat 执行列操作(平均值、分位数等)

但这似乎是一种残酷的做事方式,因为我想组合子集(例如金属和棉花的平均价格)。

我也试过了

split(data, wsearch, drop=TRUE)

但收到警告。

Warning message:
  In split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...)  
  data length is not a multiple of split variable

尝试使用lapplysplitddplysubset,但是我对R的理解不够强,无法执行。

我知道这可能很简单,但我一直坚持如何使用矩阵创建多个子集,而不是一次创建一个子集。

任何帮助都会很棒。

我看了以下

Subsetting a data.frame with an integer matrix

subsetting matrix with id from another matrix

Select observations from a subset to create a new subset based on a large dataframe in R

R Selecting column in a data frame by column in another data frame

【问题讨论】:

  • 不清楚Data的结构是什么。如果你能提供一个可重现的例子,最好是最好的,例如给我们dput(Data)dput(Matrix)的输出。
  • @flodel dput(head(data$Price)) c(17, 35, 12, 26, 1.35, 10) c("亚麻布","蜡线金属立方体", "黄铜", “亚麻布”)
  • dput(head(wsearch)) 结构(c(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, ......... .FALSE), .Dim = c(6L, 140L))
  • Data 是 data.frame 而 wsearch 是 Matrix

标签: r split binary subset sapply


【解决方案1】:

这是你想要的吗?

library(reshape2)
library(splitstackshape)

# sample data
df <- data.frame(price = c(17, 35, 12, 26, 1.35, 10),
                 material = c("linen",
                              "wax string metal cube",
                              "Metal nickel linen cotton",
                              "brass",
                              "linen",
                              "cotton silk wood"))

# split the concatenated material variable
df2 <- concat.split(data = df, split.col = "material", sep = " ", drop = TRUE)

# replace blanks with NA
df2[df2 == ""] <- NA

# melt data to long format
df3 <- melt(df2, id.vars = "price", na.rm = TRUE)

# calculate summary stats by material (= 'value' variable)
df4 <- aggregate(price ~ value, data = df3, summary)

#     value price.Min. price.1st Qu. price.Median price.Mean price.3rd Qu. price.Max.
# 1   brass     26.000        26.000       26.000     26.000        26.000     26.000
# 2  cotton     10.000        10.500       11.000     11.000        11.500     12.000
# 3    cube     35.000        35.000       35.000     35.000        35.000     35.000
# 4   linen      1.350         6.675       12.000     10.120        14.500     17.000
# 5   metal     35.000        35.000       35.000     35.000        35.000     35.000
# 6   Metal     12.000        12.000       12.000     12.000        12.000     12.000
# 7  nickel     12.000        12.000       12.000     12.000        12.000     12.000
# 8    silk     10.000        10.000       10.000     10.000        10.000     10.000
# 9  string     35.000        35.000       35.000     35.000        35.000     35.000
# 10    wax     35.000        35.000       35.000     35.000        35.000     35.000
# 11   wood     10.000        10.000       10.000     10.000        10.000     10.000

【讨论】:

  • 赢家,赢家鸡肉晚餐!我知道 Plyr 会有答案,但是融化命令让我有点害怕......这太棒了,以你的高分,这可能已经厌倦了赞美,但非常感谢......你拯救了我的周末;-)来都柏林,晚饭就在我身上!
  • @conr404,很高兴为您提供帮助!周末愉快(免费?)!干杯。
猜你喜欢
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
  • 2018-12-07
相关资源
最近更新 更多