简体   繁体   中英

Return list of factorial row combinations using 2 factors in R

I have a dataset with two identifying factors, Day (AD) and tTime (factor 1-4), and three numeric variables. I would like to return a list that contains every combination of rows that contains one of each factor,

Ex. entry one in the list would be 4 rows that have the Day x tTime factor combinations A-1, B-1, C-1, D-1. Entry two would be the 4 rows with A-2, B-1, C-1, D-1...etc, ultimately generating a list with 256 entries (I think... 4*4*4*4?).

I've worked/searched on this for a bit, but I'm very new to programming and R so don't have any tries that are even close to working to post.

Sample dataset:

testData <- data.frame(Day = rep(c("A", "B", "C", "D"), 4),
                       tTime = factor(rep(1:4, rep(4,4))),
                       numDat1 = rnorm(16, mean = 3, sd = 5),
                       numDat2 = rnorm(16, mean = 1, sd = 10),
                       numDat3 = rnorm(16, mean = 10, sd = 3))

This is pretty direct. I do wonder why you want all this repeated data - there's probably a more efficient way to go about whatever your doing (maybe making a list of row-indices and using each subset as needed?)

If your real data is much bigger, I'd suggest switching to data.table - the merge step will be much faster using keyed data tables.

x = expand.grid(A=1:4, B = 1:4, C = 1:4, D = 1:4)

result = apply(x, 1, function(r) data.frame(Day = c("A", "B", "C", "D"), tTime = r))
result = lapply(result, merge, testData)
result
# [[1]]
#   Day tTime   numDat1     numDat2   numDat3
# 1   A     1 -8.939716 -20.4731248 12.617296
# 2   B     1  8.992376  -0.2803529  8.524298
# 3   C     1  7.920297   1.2573052  7.072502
# 4   D     1 10.577459  15.9311881 13.875922
# 
# [[2]]
#   Day tTime   numDat1    numDat2   numDat3
# 1   A     2  9.689634  2.5874269  7.624269
# 2   B     1  8.992376 -0.2803529  8.524298
# 3   C     1  7.920297  1.2573052  7.072502
# 4   D     1 10.577459 15.9311881 13.875922
# 
# [[3]]
#   Day tTime   numDat1     numDat2   numDat3
# 1   A     3  6.210782 -10.5826679  7.526498
# 2   B     1  8.992376  -0.2803529  8.524298
# 3   C     1  7.920297   1.2573052  7.072502
# 4   D     1 10.577459  15.9311881 13.875922
# 
# [[4]]
#   Day tTime   numDat1    numDat2   numDat3
# 1   A     4  7.552972 10.5035948  7.999910
# 2   B     1  8.992376 -0.2803529  8.524298
# 3   C     1  7.920297  1.2573052  7.072502
# 4   D     1 10.577459 15.9311881 13.875922
# 
# [[5]]
#   Day tTime   numDat1    numDat2   numDat3
# 1   A     1 -8.939716 -20.473125 12.617296
# 2   B     2 -4.883532   6.526018 11.959817
# 3   C     1  7.920297   1.257305  7.072502
# 4   D     1 10.577459  15.931188 13.875922
# 
# [[6]]
#   Day tTime   numDat1   numDat2   numDat3
# 1   A     2  9.689634  2.587427  7.624269
# 2   B     2 -4.883532  6.526018 11.959817
# 3   C     1  7.920297  1.257305  7.072502
# 4   D     1 10.577459 15.931188 13.875922

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