简体   繁体   中英

Custom dcasting via data.table in R

Here is my data

dt = data.table(x=sample(8,20,TRUE), 
                y=sample(2,20,TRUE), 
                w = sample(letters[5:20], 20, TRUE),
                u = sample(letters[2:25], 20, TRUE),
                z=sample(letters[1:4], 20,TRUE), 
                d1 = runif(20), d2=runif(20))

Here is my dcasting code.

DC_1 = dcast.data.table(dt,x+w ~ z, value.var = "d1")

This works fine. However my data could also additionally include column 'a' and column 's' as shown below. Both of them could be included, either one, or none of them.

dt = data.table(x=sample(8,20,TRUE), 
                y=sample(2,20,TRUE), 
                w = sample(letters[5:20], 20, TRUE),
                u = sample(letters[2:25], 20, TRUE),
                z=sample(letters[1:4], 20,TRUE), 
                a = sample(letters[1:25], 20, T),
                s = sample(letters[2:17], 20, T),
                d1 = runif(20), d2=runif(20))

The additional columns however would always be characters . Also my data always has to be cast on column 'z' and value variable would always be 'd1'

How do I dcast via data.table such that it takes all the character columns (except z) available in the data.table and casts them on z?

We could subset the dataset column and use ... on the lhs of ~ to specify for all columns and on the rhs of formula it would be 'z'

dcast(dt[, setdiff(names(dt), 'd2'), with = FALSE], ... ~ z, value.var = 'd1')

Or get the column names of the character columns programmatically

nm1 <- dt[, names(which(unlist(lapply(.SD, is.character))))]
nm2 <- setdiff(nm1, 'z')
dcast(dt,paste0(paste(nm2, collapse="+"), "~ z"), value.var = 'd1')

Or another option is select from dplyr

library(dplyr) #1.0.0
dcast(dt[, select(.SD, where(is.character), d1)], ... ~ z, value.var = 'd1')

A similar option in tidyverse would be

library(tidyr)
dt %>% 
   select(where(is.character), d1) %>% 
   pivot_wider(names_from = z, values_from = d1)

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