I have a data frame with columns Price and Material, and a true/false matrix with N columns (each column is a particular type of material), and the T/F value denotes if the 'material' string appears in the data matrix
Data
Price Material
2.33 Metal nickel linen cotton
3.45 silver emerald steel
7.45 cotton silk wood
Matrix
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
...etc.
How do I create a subset the prices based on the material? So I can calculate the mean, range mode etc of prices which have the material 'Metal'.
My initial solution was to multiply the
newMat<- data$price * materialmatrix.
and then perform column operations on newMat (mean, quantile etc.)
But this seems like a brutal way of doing things, as I would like to combine the subsets (e.g average price for something with Metal && Cotton).
I also tried
split(data, wsearch, drop=TRUE)
but got the warning.
Warning message:
In split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...)
data length is not a multiple of split variable
Was trying to use lapply
, split
, ddply
, and subset
, but my understanding of R is not strong enough to execute.
I know this is probably very simple, but I am stuck on how to use the matrix to create multiple subsets, rather than create one subset at a time.
Any help would be great.
I have looked at the following
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
is. Best would be if you could please provide a reproducible example, e.g. give us the outputs ofdput(Data)
anddput(Matrix)
. – flodel