简体   繁体   中英

Subsetting using rowsum or rowSums

I am very new to R. I would like to create a list of all possible concentration combinations of a recipe that is comprise of 4 materials. The last line is where I am running into issues.

#create a sequence of numbers from 0.01 to 0.97 by 0.01
#(all possible concentration combinations for a recipe of 4 unique materials)
concs<-seq(0.01,0.97,0.01)

#create all possible permutations of these numbers with repeats
combos2<-permutations(length(concs),4,concs,TRUE,TRUE)

#subset the list of possible concentrations so that all that is left are the rows of data
#where all four values (4 columns) in a row (the four material concentrations) sum to 1
combos2<-combos2[rowSums(combos2[,1:4])==1]

How about creating a subsetting vector such as this:

#create a sequence of numbers from 0.01 to 0.97 by 0.01
#(all possible concentration combinations for a recipe of 4 unique materials)
concs<-seq(0.01,0.97,0.01)

#create all possible permutations of these numbers with repeats
combos2<-gtools::permutations(length(concs),4,concs,TRUE,TRUE)

#subset the list of possible concentrations so that all that is left are the rows of data
#where all four values (4 columns) in a row (the four material concentrations) sum to 1

# Subset vector to only retain the rows where the sum is equal to 1
subset_vctr <- which(Rfast::rowsums(combos2[, 1:4]) == 1)
combos2<-combos2[subset_vctr, ]

I am essentially just asking which of the row sums are equal to 1 and then using that vector to subset the matrix combos2 . The Rfast package contains fast routines for working on matrices.

Here's a base R solution:

combos2 <- subset(combos2, rowSums(combos2[, 1:4]) == 1)

head(combos2)
     [,1] [,2] [,3] [,4]
[1,] 0.01 0.01 0.01 0.97
[2,] 0.01 0.01 0.02 0.96
[3,] 0.01 0.01 0.03 0.95
[4,] 0.01 0.01 0.04 0.94
[5,] 0.01 0.01 0.05 0.93
[6,] 0.01 0.01 0.06 0.92

Here is a probably quicker way, avoiding the use of permutations ...

combos2 <- expand.grid(seq(0, .97, 0.01),  #combinations of first three variables
                       seq(0, .97, 0.01),
                       seq(0, .97, 0.01))

combos2$Var4 <- 1 - rowSums(combos2)       #define fourth variable to sum to 1
combos2 <- combos2[combos2$Var4 >= 0, ]    #delete any rows with Var4<0

For your inormation, Rfast also contains fast functions with permutations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM