r - Unboxing List of lists as Data Frame columns elegantly -
i have data frame contains file name regular parts. use regex parse file name , store each part in own column.
parse.file.name <- function(file.name="cc-nolabel-aemnz334_0009-loc-1317-407-6-39.png") { rfn <- regexec(pattern="cc-(.+?)-(.+?)-loc-(.+?)-(.+?)-(.+?)-(.+?)\\.png", text=file.name) matchfn <- regmatches(file.name, rfn) return(matchfn) } basic.features$parsed.filename <- parse.file.name(as.character(basic.features$filename))
filename
contains values similar default parameter. i'm retrieving individual values each column following:
basic.features$label <- unlist(lapply(basic.features$parsed.filename, function(pf) { return(unlist(pf)[2]) } ))
i feel not elegant way couldn't manage individual values data frame column contains list in each row easily. there better way this?
if example data:
basic.features <- data.frame(filename=c("cc-nolabel-aemnz336_0009-loc-1003-1504-7-8.png", "cc-nolabel-aemnz335_0006-loc-1979-880-13-10.png", "cc-nolabel-aemnz333_0007-loc-941-263-8-8.png", "cc-nolabel-aemnz336_0014-loc-2011-24-4-4.png", "cc-nolabel-aemnz335_0013-loc-2087-644-66-41.png", "cc-nolabel-aemnz333_0013-loc-1531-374-12-23.png"))
it's simpler if use sapply
:
basic.features$label <- sapply(basic.features$parsed.filename,function(x){x[2]})
however, if want turn parsed values data.frame in 1 shot, this:
df <- data.frame(t(sapply(basic.features$parsed.filename,function(x){x}))) colnames(df) <- c('filename','label','code1','code2','code3','code4','code5') > df filename label code1 code2 code3 code4 code5 1 cc-nolabel-aemnz336_0009-loc-1003-1504-7-8.png nolabel aemnz336_0009 1003 1504 7 8 2 cc-nolabel-aemnz335_0006-loc-1979-880-13-10.png nolabel aemnz335_0006 1979 880 13 10 3 cc-nolabel-aemnz333_0007-loc-941-263-8-8.png nolabel aemnz333_0007 941 263 8 8 4 cc-nolabel-aemnz336_0014-loc-2011-24-4-4.png nolabel aemnz336_0014 2011 24 4 4 5 cc-nolabel-aemnz335_0013-loc-2087-644-66-41.png nolabel aemnz335_0013 2087 644 66 41 6 cc-nolabel-aemnz333_0013-loc-1531-374-12-23.png nolabel aemnz333_0013 1531 374 12 23
Comments
Post a Comment