Question

I have a data.table with 18050 columns, I need to subset my table every 50 columns and find the modal outcome of "myrowname" which contains character. What I do right now is:

library(data.table)
library(raster)
mydatatable <- fread(file.choose(), sep=",", verbose=T, header=F)
mydatatable <- setkey(mydatatable,V1) # V1 contains the names of the rows
for(i in seq(50,18050,by=50)){
res.list <- as.character(mydatatable["myrowname",(i-49):i, with=FALSE])
    print(modal(res.list))
    # do other stuff...
}

this works of course, but I am wondering if there is a more quick way (maybe faster) to subset the data.table.

EDIT some sample data with just the row that is going to be subsetted, please tell me if this is sufficient:

structure(list(V1 = "results", V1 = "results", V51 = "star", 
V101 = "null", V151 = "null", V201 = "null", V251 = "null", 
V301 = "null", V351 = "null", V401 = "null", V451 = "null", 
V501 = "null", V551 = "null", V601 = "null", V651 = "null", 
V701 = "null", V751 = "null", V801 = "null", V851 = "null", 
V901 = "null", V951 = "null", V1001 = "complete", V1051 = "star", 
V1101 = "null", V1151 = "null", V1201 = "null", V1251 = "null", 
V1301 = "null", V1351 = "null", V1401 = "null", V1451 = "null", 
V1501 = "null", V1551 = "null", V1601 = "null", V1651 = "null", 
V1701 = "null", V1751 = "null", V1801 = "null", V1851 = "null", 
V1901 = "null", V1951 = "complete", V2001 = "complete", V2051 = "star", 
V2101 = "null", V2151 = "null", V2201 = "null", V2251 = "null", 
V2301 = "null", V2351 = "null", V2401 = "null", V2451 = "null", 
V2501 = "null", V2551 = "null", V2601 = "null", V2651 = "null", 
V2701 = "null", V2751 = "null", V2801 = "null", V2851 = "null", 
V2901 = "complete", V2951 = "complete", V3001 = "complete", 
V3051 = "star", V3101 = "null", V3151 = "null", V3201 = "null", 
V3251 = "null", V3301 = "null", V3351 = "null", V3401 = "null", 
V3451 = "null", V3501 = "null", V3551 = "null", V3601 = "null", 
V3651 = "null", V3701 = "null", V3751 = "null", V3801 = "null", 
V3851 = "complete", V3901 = "complete", V3951 = "complete", 
V4001 = "star", V4051 = "star", V4101 = "null", V4151 = "null", 
V4201 = "null", V4251 = "null", V4301 = "null", V4351 = "null", 
V4401 = "null", V4451 = "null", V4501 = "null", V4551 = "null", 
V4601 = "null", V4651 = "null", V4701 = "null", V4751 = "null", 
V4801 = "complete", V4851 = "complete", V4901 = "complete", 
V4951 = "complete", V5001 = "star", V5051 = "star", V5101 = "null", 
V5151 = "null", V5201 = "null", V5251 = "null", V5301 = "null", 
V5351 = "null", V5401 = "null", V5451 = "null", V5501 = "null", 
V5551 = "null", V5601 = "null", V5651 = "null", V5701 = "null", 
V5751 = "complete", V5801 = "complete", V5851 = "complete", 
V5901 = "complete", V5951 = "star", V6001 = "star", V6051 = "star", 
V6101 = "null", V6151 = "null", V6201 = "null", V6251 = "null", 
V6301 = "null", V6351 = "null", V6401 = "null", V6451 = "null", 
V6501 = "null", V6551 = "null", V6601 = "null", V6651 = "null", 
V6701 = "complete", V6751 = "complete", V6801 = "complete", 
V6851 = "complete", V6901 = "star", V6951 = "star", V7001 = "star", 
V7051 = "star", V7101 = "null", V7151 = "null", V7201 = "null", 
V7251 = "null", V7301 = "null", V7351 = "null", V7401 = "null", 
V7451 = "null", V7501 = "null", V7551 = "null", V7601 = "null", 
V7651 = "complete", V7701 = "complete", V7751 = "complete", 
V7801 = "complete", V7851 = "star", V7901 = "star", V7951 = "star", 
V8001 = "star", V8051 = "star", V8101 = "null", V8151 = "null", 
V8201 = "null", V8251 = "null", V8301 = "null", V8351 = "null", 
V8401 = "null", V8451 = "null", V8501 = "null", V8551 = "null", 
V8601 = "complete", V8651 = "complete", V8701 = "complete", 
V8751 = "complete", V8801 = "near-complete", V8851 = "star", 
V8901 = "star", V8951 = "star", V9001 = "star", V9051 = "star", 
V9101 = "null", V9151 = "null", V9201 = "null", V9251 = "null", 
V9301 = "null", V9351 = "null", V9401 = "null", V9451 = "null", 
V9501 = "null", V9551 = "complete", V9601 = "complete", V9651 = "complete", 
V9701 = "complete", V9751 = "star", V9801 = "star", V9851 = "star", 
V9901 = "star", V9951 = "star", V10001 = "star", V10051 = "star", 
V10101 = "null", V10151 = "null", V10201 = "null", V10251 = "null", 
V10301 = "null", V10351 = "null", V10401 = "null", V10451 = "null", 
V10501 = "complete", V10551 = "complete", V10601 = "complete", 
V10651 = "complete", V10701 = "star", V10751 = "star", V10801 = "star", 
V10851 = "star", V10901 = "star", V10951 = "star", V11001 = "star", 
V11051 = "star", V11101 = "null", V11151 = "null", V11201 = "null", 
V11251 = "null", V11301 = "null", V11351 = "null", V11401 = "null", 
V11451 = "complete", V11501 = "complete", V11551 = "complete", 
V11601 = "complete", V11651 = "star", V11701 = "star", V11751 = "star", 
V11801 = "star", V11851 = "star", V11901 = "star", V11951 = "star", 
V12001 = "star", V12051 = "star", V12101 = "null", V12151 = "null", 
V12201 = "null", V12251 = "null", V12301 = "null", V12351 = "null", 
V12401 = "complete", V12451 = "complete", V12501 = "complete", 
V12551 = "complete", V12601 = "star", V12651 = "star", V12701 = "star", 
V12751 = "star", V12801 = "star", V12851 = "star", V12901 = "star", 
V12951 = "star", V13001 = "star", V13051 = "star", V13101 = "null", 
V13151 = "null", V13201 = "null", V13251 = "null", V13301 = "null", 
V13351 = "complete", V13401 = "complete", V13451 = "complete", 
V13501 = "star", V13551 = "star", V13601 = "star", V13651 = "star", 
V13701 = "star", V13751 = "star", V13801 = "star", V13851 = "star", 
V13901 = "star", V13951 = "star", V14001 = "star", V14051 = "star", 
V14101 = "null", V14151 = "null", V14201 = "null", V14251 = "null", 
V14301 = "complete", V14351 = "complete", V14401 = "complete", 
V14451 = "star", V14501 = "star", V14551 = "star", V14601 = "star", 
V14651 = "star", V14701 = "star", V14751 = "star", V14801 = "star", 
V14851 = "star", V14901 = "star", V14951 = "star", V15001 = "star", 
V15051 = "star", V15101 = "null", V15151 = "null", V15201 = "null", 
V15251 = "complete", V15301 = "complete", V15351 = "star", 
V15401 = "star", V15451 = "star", V15501 = "star", V15551 = "star", 
V15601 = "star", V15651 = "star", V15701 = "star", V15751 = "star", 
V15801 = "star", V15851 = "star", V15901 = "star", V15951 = "star", 
V16001 = "star", V16051 = "star", V16101 = "null", V16151 = "null", 
V16201 = "complete", V16251 = "star", V16301 = "star", V16351 = "star", 
V16401 = "star", V16451 = "star", V16501 = "star", V16551 = "star", 
V16601 = "star", V16651 = "star", V16701 = "star", V16751 = "star", 
V16801 = "star", V16851 = "star", V16901 = "star", V16951 = "star", 
V17001 = "star", V17051 = "star", V17101 = "null", V17151 = "star", 
V17201 = "star", V17251 = "star", V17301 = "star", V17351 = "star", 
V17401 = "star", V17451 = "star", V17501 = "star", V17551 = "star", 
V17601 = "star", V17651 = "star", V17701 = "star", V17751 = "star", 
V17801 = "star", V17851 = "star", V17901 = "star", V17951 = "star", 
V18001 = "star"), .Names = c("V1", "V1", "V51", "V101", "V151", 
"V201", "V251", "V301", "V351", "V401", "V451", "V501", "V551", 
"V601", "V651", "V701", "V751", "V801", "V851", "V901", "V951", 
"V1001", "V1051", "V1101", "V1151", "V1201", "V1251", "V1301", 
"V1351", "V1401", "V1451", "V1501", "V1551", "V1601", "V1651", 
"V1701", "V1751", "V1801", "V1851", "V1901", "V1951", "V2001", 
"V2051", "V2101", "V2151", "V2201", "V2251", "V2301", "V2351", 
"V2401", "V2451", "V2501", "V2551", "V2601", "V2651", "V2701", 
"V2751", "V2801", "V2851", "V2901", "V2951", "V3001", "V3051", 
"V3101", "V3151", "V3201", "V3251", "V3301", "V3351", "V3401", 
"V3451", "V3501", "V3551", "V3601", "V3651", "V3701", "V3751", 
"V3801", "V3851", "V3901", "V3951", "V4001", "V4051", "V4101", 
"V4151", "V4201", "V4251", "V4301", "V4351", "V4401", "V4451", 
"V4501", "V4551", "V4601", "V4651", "V4701", "V4751", "V4801", 
"V4851", "V4901", "V4951", "V5001", "V5051", "V5101", "V5151", 
"V5201", "V5251", "V5301", "V5351", "V5401", "V5451", "V5501", 
"V5551", "V5601", "V5651", "V5701", "V5751", "V5801", "V5851", 
"V5901", "V5951", "V6001", "V6051", "V6101", "V6151", "V6201", 
"V6251", "V6301", "V6351", "V6401", "V6451", "V6501", "V6551", 
"V6601", "V6651", "V6701", "V6751", "V6801", "V6851", "V6901", 
"V6951", "V7001", "V7051", "V7101", "V7151", "V7201", "V7251", 
"V7301", "V7351", "V7401", "V7451", "V7501", "V7551", "V7601", 
"V7651", "V7701", "V7751", "V7801", "V7851", "V7901", "V7951", 
"V8001", "V8051", "V8101", "V8151", "V8201", "V8251", "V8301", 
"V8351", "V8401", "V8451", "V8501", "V8551", "V8601", "V8651", 
"V8701", "V8751", "V8801", "V8851", "V8901", "V8951", "V9001", 
"V9051", "V9101", "V9151", "V9201", "V9251", "V9301", "V9351", 
"V9401", "V9451", "V9501", "V9551", "V9601", "V9651", "V9701", 
"V9751", "V9801", "V9851", "V9901", "V9951", "V10001", "V10051", 
"V10101", "V10151", "V10201", "V10251", "V10301", "V10351", "V10401", 
"V10451", "V10501", "V10551", "V10601", "V10651", "V10701", "V10751", 
"V10801", "V10851", "V10901", "V10951", "V11001", "V11051", "V11101", 
"V11151", "V11201", "V11251", "V11301", "V11351", "V11401", "V11451", 
"V11501", "V11551", "V11601", "V11651", "V11701", "V11751", "V11801", 
"V11851", "V11901", "V11951", "V12001", "V12051", "V12101", "V12151", 
"V12201", "V12251", "V12301", "V12351", "V12401", "V12451", "V12501", 
"V12551", "V12601", "V12651", "V12701", "V12751", "V12801", "V12851", 
"V12901", "V12951", "V13001", "V13051", "V13101", "V13151", "V13201", 
"V13251", "V13301", "V13351", "V13401", "V13451", "V13501", "V13551", 
"V13601", "V13651", "V13701", "V13751", "V13801", "V13851", "V13901", 
"V13951", "V14001", "V14051", "V14101", "V14151", "V14201", "V14251", 
"V14301", "V14351", "V14401", "V14451", "V14501", "V14551", "V14601", 
"V14651", "V14701", "V14751", "V14801", "V14851", "V14901", "V14951", 
"V15001", "V15051", "V15101", "V15151", "V15201", "V15251", "V15301", 
"V15351", "V15401", "V15451", "V15501", "V15551", "V15601", "V15651", 
"V15701", "V15751", "V15801", "V15851", "V15901", "V15951", "V16001", 
"V16051", "V16101", "V16151", "V16201", "V16251", "V16301", "V16351", 
"V16401", "V16451", "V16501", "V16551", "V16601", "V16651", "V16701", 
"V16751", "V16801", "V16851", "V16901", "V16951", "V17001", "V17051", 
"V17101", "V17151", "V17201", "V17251", "V17301", "V17351", "V17401", 
"V17451", "V17501", "V17551", "V17601", "V17651", "V17701", "V17751", 
"V17801", "V17851", "V17901", "V17951", "V18001"), row.names = c(NA, 
-1L), class = c("data.table", "data.frame"), sorted = "V1", .internal.selfref = <pointer:     0x7fd3b20d7578>)
Was it helpful?

Solution

EDIT You're operating on a single vector, so it's unnecessary to subset the data.table more than once.

# Get the row vector of interest
rowvec<-as.character(as.vector(mydatatable["results", 2:18050])) 

#Return Mode of vector for every 50 elements (same as every 50 columns)
sapply(seq(50,length(rowvec),by=50), function (i) modal(rowvec[(i-49):i]))

OTHER TIPS

I would suggest you transform the data to a long format using melt.data.table.

library(reshape2)
library(data.table)
library(raster)
mydatatable <- fread(file.choose(), sep=",", verbose=T, header=F)
mydatatable <- setkey(mydatatable,V1) # V1 contains the names of the rows

mydatatable <- melt(mydataframe)  ## V1 as id, all others as levels of variable

# create bins based on numeric value in variable
mydatatable[, bin:=as.integer(substr(variable, 2, nchar(variable))) %/% 50)]

# define your procedure as a function that takes a vector of entries from V2-V18050
myfunction <- function(x) ...

setkey(mydatatable, V1, bin)
mydatatable[J('myrowname'), function(value), by=bin] 

It is not possible to test the validity of this code without a minimal example, but this should at least give you an idea.

I can't compare by speed unless you provide some sample code, but I would bet this would speed things up nicely:

res.list <- mydatatable['myrowname', .SD, .SDcols=seq(1,1850,50)]

print(modal(as.character(res.list)))

In the data.table syntax, .SD represents the grouped data.table (here it is just the entire data table since you aren't using any groupings with by=) and .SDcols specifies which columns are to be inluded in each .SD.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top