Using the Character of a Range in Subset()/Coercing Range from Character to Numeric - r

I'm struggling with having the subset() function use a range (i.e. 4:7) that is being called as a character from a variable.
Is there a way for me to coerce the input, which is the variable DayVar and has different days I want the function to subset, to be numeric while avoiding the following issues:
1.) keeping the 4:7 as such instead of as 4, 5, 6, 7, and
2.) converting the character "1:4" into numeric format that the subset evaluation can use as though it were 1:4.
Here is a sample data frame:
DayVar = c("1", "2", "3", "4:7")
a <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
b <- c(61:70)
Day <- c(1:10)
df <- data.frame("a" = a, "b" = b, "Day" = Day)
Subset <- list()
for(i in 1:length(DayVar)){
Subset[[i]] = subset(df, Day %in% DayVar[i])
}

As thelatemail suggested the list works but you have to change the DayVar quotes to get the list index:
DayVar <- list(1,2,3,4:7)
Subset <- list()
for(i in 1:length(DayVar)){
Subset[[i]] = subset(df, Day %in% DayVar[[i]])
}

Related

R add all combinations of three values of a vector to a three-dimensional array

I have a data frame with two columns. The first one "V1" indicates the objects on which the different items of the second column "V2" are found, e.g.:
V1 <- c("A", "A", "A", "A", "B", "B", "B", "C", "C", "C", "C")
V2 <- c("a","b","c","d","a","c","d","a","b","d","e")
df <- data.frame(V1, V2)
"A" for example contains "a", "b", "c", and "d". What I am looking for is a three dimensional array with dimensions of length(unique(V2)) (and the names "a" to "e" as dimnames).
For each unique value of V1 I want all possible combinations of three V2 items (e.g. for "A" it would be c("a", "b", "c"), c("a", "b", "d", and c("b", "c", "d").
Each of these "three-item-co-occurrences" should be regarded as a coordinate in the three-dimensional array and therefore be added to the frequency count which the values in the array should display. The outcome should be the following array
ar <- array(data = c(0,0,0,0,0,0,0,1,2,1,0,1,0,2,0,0,2,2,0,1,0,1,0,1,0,
0,0,1,2,1,0,0,0,0,0,1,0,0,1,0,2,0,1,0,1,1,0,0,1,0,
0,1,0,2,0,1,0,0,1,0,0,0,0,0,0,2,1,0,0,0,0,0,0,0,0,
0,2,2,0,1,2,0,1,0,1,2,1,0,0,0,0,0,0,0,0,1,1,0,0,0,
0,1,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0),
dim = c(5, 5, 5),
dimnames = list(c("a", "b", "c", "d", "e"),
c("a", "b", "c", "d", "e"),
c("a", "b", "c", "d", "e")))
I was wondering about the 3D symmetry of your result. It took me a while to understand that you want to have all permutations of all combinations.
library(gtools) #for the permutations
foo <- function(x) {
#all combinations:
combs <- combn(x, 3, simplify = FALSE)
#all permutations for each of the combinations:
combs <- do.call(rbind, lapply(combs, permutations, n = 3, r = 3))
#tabulate:
do.call(table, lapply(asplit(combs, 2), factor, levels = letters[1:5]))
}
#apply grouped by V1, then sum the results
res <- Reduce("+", tapply(df$V2, df$V1, foo))
#check
all((res - ar)^2 == 0)
#[1] TRUE
I used to use the crossjoin CJ() to retain the pairwise count of all combinations of two different V2 items
res <- setDT(df)[,CJ(unique(V2), unique(V2)), V1][V1!=V2,
.N, .(V1,V2)][order(V1,V2)]
This code creates a data frame res with three columns. V1 and V2 contain the respective items of V2 from the original data frame df and N contains the count (how many times V1 and V2 appear with the same value of V1 (from the original data frame df).
Now, I found that I could perform this crossjoin with three 'dimensions' as well by just adding another unique(V2) and adapting the rest of the code accordingly.
The result is a data frame with four columns. V1, V2, and V3 indicate the original V2 items and N again shows the number of mutual appearances with the same original V1 objects.
res <- setDT(df)[,CJ(unique(V2), unique(V2), unique(V2)), V1][V1!=V2 & V1 != V3 & V2 != V3,
.N, .(V1,V2,V3)][order(V1,V2,V3)]
The advantage of this code is that all empty combinations (those which do not appear at all) are not considered. It worked with 1,000,000 unique values in V1 and over 600 unique items in V2, which would have otherwise caused an extremely large array of 600 x 600 x 600

How to sort a data frame on multiple variables of which the names are given in vectors using a base R function?

I have a data frame like the one below:
df <- data.frame(v1 = c("A", "B", "A", "A", "B", "B", "B", "B", "A", "A", "A", "A"),
v2 = c("X", "Y", "X", "Y", "Z", "X", "X", "Y", "X", "Y", "Z", "Z"),
v3 = c(2, 1, 3, 1, 1, 2, 1, 2, 1, 2, 2, 1))
In this data frame v1 and v2 are so called grouping variables (charachter vectors is this case) within I'd like to order my counter variable v3 ascending using (a) base R function(s). There's no requirement for the order in which the grouping variables are sorted (both ascending and descending would be ok). Now in this special case that would be easy:
df <- df[order(df$v1, df$v2, df$v3),]
Or alternatively:
df <- df[do.call(what = order, args = df),]
What I'd like is a more general solution for any data frame with n grouping variables of which the names are contained in a vector and the name of the counter variable is contained in another vector. Reason I want this is that this data is given in a function call in a user defined function and can therefore vary.
grouping_vars <- c("v1", "v2", ..., "vn") #not actual code. Data frame contains *n* variables.
counter <- "vi" #not actual code. One of them, the i-th, is the counter variable.
Again, I'd like to make use of a base R function here (most likely order) and not a solution from data.frame or tidyverse from example.
Your code is almost there. Just use [] behind df to extract grouping and numerical columns for ordering.
df[do.call(what = order, args = df[,c(grouping_vars, counter)]), ]
PeterD: I added a comma in front of the vector that contains the selected columns to be explicit about the selection of columns of data frame df.

Subset a Data Frame Based on All Combinations and Sub-combinations of Factor Variables

I need to subset a data.frame based on all combinations an sub-combinations of multiple columns of factor variables. Additionally the number of columns factor variables may change so the method needs to be flexible in accepting different numbers of attributes. I can figure out how to create the combinations of variables in a simple example but don't have a good way to subset the data.frame efficiently. Any thoughts?
#setup an example data.frame
a <- c("a", "b", "b", "b", "e")
b <- c("b", "c", "b", "b", "f")
c <- c("c", "d", "b", "b", "g")
df <- data.table(a = a, b = b, c = c)
#build a data.frame of unique combos to subset on
df_unique <- df[!duplicated(df), ]
df_combos <- data.table()
for(i in 1:ncol(df_unique)){
for(x in 1:ncol(df_unique)){
df_sub <- df_unique[,i:x, with = F]
df_combos <- rbind(df_combos, df_sub, fill = T)
}
}
df_combos <- df_combos[!duplicated(df_combos), ]
rm(df_unique)
#create a loop to build the subsets
combos_out <- data.table()
for(i in 1:nrow(df_combos)){
df_combos_sub <- df_combos[i, ]
df_combos_sub <- df_combos_sub[,which(unlist(lapply(df_combos_sub, function(x)!all(is.na(x))))),with=F]
df_sub <- merge(df, df_combos_sub, by = colnames(df_combos_sub))
#interesting code here that performs analysis on the subsets
}

Preserving zero length groups with aggregate

I just noticed that aggregate disappears empty groups from the result, how can I solve this? e.g.
`xx <- c("a", "b", "d", "a", "d", "a")
xx <- factor(xx, levels = c("a", "b", "c", "d"))
y <- rnorm(60, 5, 1)
z <- matrix(y, 6, 10)
aggregate(z, by = list(groups = xx), sum)`
xx is a factor variable with 4 levels, but the result gives just 3 rows, and would like a row for the "c" level with zeros. I would like the same behavior of table(xx) tha gives frecuencies even for levels with no observations.
We can create another data.frame with just the levels of 'xx' and then merge with the aggregate. The output will have all the 'groups' while the row corresponding to the missing level for the other columns will be NA.
merge(data.frame(groups=levels(xx)),
aggregate(z, by = list(groups = xx), sum), all.x=TRUE)
Another option might be to convert to 'long' format with melt and then use dcast with fun.aggregate as 'sum' and drop=FALSE
library(data.table)
dcast(melt(data.table(groups=xx, z), id.var='groups'),
groups~variable, value.var='value', sum, drop=FALSE)

Order data frame by two columns in R

I'm trying to reorder the rows of a data frame by two factors. For the first factor i'm happy with the default ordering. For the second factor i'd like to impose my own custom order to the rows. Here's some dummy data:
dat <- data.frame(apple=rep(LETTERS[1:10], 3),
orange=c(rep("agg", 10), rep("org", 10), rep("fut", 10)),
pear=rnorm(30, 10),
grape=rnorm(30, 10))
I'd like to order "apple" in a specific way:
appleOrdered <- c("E", "D", "J", "A", "F", "G", "I", "B", "H", "C")
I've tried this:
dat <- dat[with(dat, order(orange, rep(appleOrdered, 3))), ]
But it seems to put "apple" into a random order. Any suggestions? Thanks.
Reordering the factor levels:
dat[with(dat, order(orange, as.integer(factor(apple, appleOrdered)))), ]
Try using a factor with the levels in the desired order and the arrange function from plyr:
dat$apple <- factor(dat$apple,levels=appleOrdered)
arrange(dat,orange,apple)

Resources