I am trying to compare and plot a variable's distribution with that of the variable's log transformation.
What I am saying below, for every variable in mtcars if it is either a numeric or an integer return a histogram of it, then return a histogram with a log transformation, so that I can compare.
Any help would be appreciated.
for(i in ncol(mtcars)){
par(mfcol = c(1,2))
if (as.numeric | as.integer(mtcars[,i]) == T){
return(hist(mtcars[,i]))}
if (as.numeric | as.integer(mtcars[,i]) == T){
return(hist(log(mtcars[,i])+1))}
}
Error in as.numeric | as.integer(mtcars[, i]) == T :
operations are possible only for numeric, logical or complex types
This has nothing to do with hist()
, it's the if
statement that does not make much sense.
is.numeric()
and is.integer()
is.numeric(mtcars[,i])
and is.integer(mtcars[,i])
. is.numeric()
and is.integer()
already return a boolean
, so there is no need to check == T
Your code should read:
for(i in ncol(mtcars)){
if (is.numeric(mtcars[,i]) | is.integer(mtcars[,i])){
return(hist(mtcars[,i]))
return(hist(log(mtcars[,i])+1)}
}
You should also know that it's almost always better to leverage the apply
functions family instead of loops, eg:
apply(mtcars, 2, function(x) {hist(log(x)+1); hist(x)})
You should use function is.numeric
and is.integer
. Using as.
makes no sense in an if statement.
This would be the correct approach:
for(i in ncol(mtcars)){
par(mfcol = c(1,2))
if (is.numeric(mtcars[,i] | is.integer(mtcars[,i])){
return(hist(mtcars[,i]))
}
else {
return(hist(log(mtcars[,i])+1))
}
}
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.