r - apply prepends space for logical -
having strange issue here apply , r 3.0.1.
i have huge dataframe text, numbers , logical values. logical values converted chr when use apply, because r allows true == "true" isn't problem.
but logical values, apply seems prepend space, , true == " true" returns na. of course, do
sapply(cuelist[,4],fun=function(logicalvalue) as.logical(sub("^ +", "", logicalvalue))) but isn't nice , still don't know why r that.
df <- data.frame(test=c("a","b","<",">"),logi=c(true,false,false,true)) apply(df, margin=1, function(listelement) print(listelement) ) interestlingly, spaces appear in example on [2,1] , [2,4]
version _
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 0.1
year 2013
month 05
day 16
svn rev 62743
language r
version.string r version 3.0.1 (2013-05-16) nickname sport
edit: same behaviour on r version 2.15.0 (2012-03-30)
edit2: dataframe lools this
> df test logi 1 false 2 b false 3 < true 4 > true > str(df) 'data.frame': 4 obs. of 2 variables: $ test: factor w/ 4 levels "<",">","a","b": 3 4 1 2 $ logi: logi false false true true
in way, problem apply, more appropriately, problem as.matrix, , how handling logical values.
here few examples elaborate on query had karl.
first, let's create 4 data.frames tests on.
- your original
data.framedemonstrate behavior: - a
data.framevarying number of characters in "test" column karl's explanation of what's going on. - a
data.framenumbers start understand seems going on. - a
data.frame"logi" column explicitly createdas.character.
df1 <- data.frame(test = c("a","b","<",">"), logi = c(true,false,false,true)) df2 <- data.frame(test = c("aa","b","<",">>"), logi = c(true,false,false,true)) df3 <- data.frame(test = c("aa","b","<",">>"), logi = c(true,false,false,true), num = c(1, 12, 123, 2)) df4 <- data.frame(test = c("aa","b","<",">>"), logi = as.character(c(true,false,false,true))) now, let's use as.matrix on each of them.
this has space before true.
as.matrix(df1) # test logi # [1,] "a" " true" # [2,] "b" "false" # [3,] "<" "false" # [4,] ">" " true" this has space before true, "test" column remains unaffected. hmm.
as.matrix(df2) # test logi # [1,] "aa" " true" # [2,] "b" "false" # [3,] "<" "false" # [4,] ">>" " true" ahh... has space before true and spaces before shorter numbers. seems perhaps r considering numeric underlying value of true , false, calculating width of number of characters in true , false. again, first "test" column remains unaffected.
as.matrix(df3) # test logi num # [1,] "aa" " true" " 1" # [2,] "b" "false" " 12" # [3,] "<" "false" "123" # [4,] ">>" " true" " 2" things seem fine here, if tell r logi column character column.
as.matrix(df4) # test logi # [1,] "aa" "true" # [2,] "b" "false" # [3,] "<" "false" # [4,] ">>" "true" for it's worth, sapply doesn't seem have problem.
sapply(df1, as.matrix) # test logi # [1,] "a" "true" # [2,] "b" "false" # [3,] "<" "false" # [4,] ">" "true" update
in r public chat room, joshua ulrich points format being culprit. as.matrix uses as.vector factors, converts them character (try str(as.vector(df1$test)) see mean; else, uses format, unfortunately, doesn't have option include of arguments format, 1 of trim (which default set false).
compare following:
a <- c(true, false) format(a) # [1] " true" "false" format(a, trim = true) # [1] "true" "false" format(as.character(a)) # [1] "true " "false" format(as.factor(a)) # [1] "true " "false" so, how sort of convert logical columns character? maybe (though suggest creating backup of data first):
df1[sapply(df1, is.logical)] <- lapply(df1[sapply(df1, is.logical)], as.character) df1 # test logi # 1 true # 2 b false # 3 < false # 4 > true as.matrix(df1) # test logi # [1,] "a" "true" # [2,] "b" "false" # [3,] "<" "false" # [4,] ">" "true"
Comments
Post a Comment