I'm pretty new to R and trying to write a loop or a concise code for a simple task: checking the class of all the current objects in my Global Environment in R.
class(mydata)
#[1] "data.frame"
class(mylist)
#[1] "list"
class(mymatrix)
#[1] "matrix"
...
The following code worked, but what if I have many objects and I don't want to type all the names.
dflist <- list(mydata, mylist, mymatrix)
lapply(dflist,class)
I tried the following methods, none of them worked.
#1
for (i in ls()){
class(i)
}
#2
for (i in ls()){
lapply(i,class)
}
any solutions? Thanks.
You could use mget which returns "a named list of objects". The function's first argument should be a character vector of object names, which is what ls() returns.
lapply(mget(ls()), class)
#$mydata
#[1] "data.frame"
#
#$mylist
#[1] "list"
#
#$mymatrix
#[1] "matrix"
Try eapply:
eapply(.GlobalEnv, class)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With