Skip to content
Prev 347999 / 398500 Next

MApply and SubStr

On Feb 10, 2015, at 3:58 PM, Brian Trautman wrote:

            
It appears from you code that you might be expecting a vector in a dataframe object to have a character mode in the first postition and a numeric mode in the second position. That wouldn't seem to be a reasonable expectation. But maybe you were hoping the chr and num types were to be applied to columns. I was surprised to get something different from as.data.table:
Classes ?data.table? and 'data.frame':	2 obs. of  2 variables:
 $ V1: Factor w/ 2 levels "aa","bb": 1 2
 $ NA: Factor w/ 2 levels "1","2": 1 2
 - attr(*, ".internal.selfref")=<externalptr> 

The mapply operation made a matrix which forces all values to be the same mode:
+  startpos, endpos, typelist) )
 chr [1:2, 1:2] "aa" "bb" "1" "2"
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "V1" NA

You might have gotten something less homogeneous if you added the SIMPLIFY argument:
+  startpos, endpos, typelist, SIMPLIFY=FALSE) )
List of 2
 $ V1: chr [1:2] "aa" "bb"
 $ NA: num [1:2] 1 2
David Winsemius
Alameda, CA, USA