Is there anyone who can help me? please
I have made tons of dataset in R studio like you could see in the right part of the image below and I would like to make a simple table like a summary which contains only "name, number of observation and number of variables" of all of each dataset. (I want the table to be made just like the RIGHT PART OF THE IMAGE below)
Hopefully the table should be in excel or csv form.
Here is another approach. The lines where I load the data sets mtcars, iris and iris3 are only there to provide some objects for the reproducible example. You can write the object TBL as a csv or an Excel file.
library(tibble)
library(purrr)
M <- mtcars
I <- iris
I3 <- iris3 #not a data frame
DataNames <- ls()
GetInfo <- function(x) {
Obj <- get(x)
if (grepl("data.frame", class(Obj))) {
Name <- x
Obs <- nrow(Obj)
Vars <- ncol(Obj)
} else {
Name <- x
Obs <- NA
Vars <- NA
}
tibble(Name = Name, Observations = Obs, Variables = Vars)
}
TBL <- map_dfr(DataNames, GetInfo )
TBL
#> # A tibble: 3 x 3
#> Name Observations Variables
#> <chr> <int> <int>
#> 1 I 150 5
#> 2 I3 NA NA
#> 3 M 32 11
thank you for your help. it runs well but the thing is it shows me all the values as NA (
I tell you this hopefully it could be a hint to you. All of the dataset I made in R studio, they are made with a package dplyr(tbl_df).
Please don't use images to communicate code and console outputs. It's all text in RStudio so copy and paste the text into here. Use three backticks on their own line to format code as code
So its nice to read
Ok. With that out if the way, I think it's a mistake to grepl the result of class. Rather use is.dataframe function to test for that more directly. Tibbles have 3 classes after all.
I don't know why it isn't work even they all are dataframe like you could see below.
> library(tibble)
> library(purrr)
> DataNames <- ls()
> GetInfo <- function(x) {
+ Obj <- get(x)
+ if (grepl("data.frame", class(Obj))) {
+ Name <- x
+ Obs <- nrow(Obj)
+ Vars <- ncol(Obj)
+ } else {
+ Name <- x
+ Obs <- NA
+ Vars <- NA
+ }
+ tibble(Name = Name, Observations = Obs, Variables = Vars)
+
+ }
> TBL <- map_dfr(DataNames, GetInfo )
> TBL
# A tibble: 160 x 3
Name Observations Variables
<chr> <lgl> <lgl>
1 art_No NA NA
2 art_Yes NA NA
3 Before_sleep_music NA NA
4 Before_sleep_nothing NA NA
5 Before_sleep_PC NA NA
6 Before_sleep_smartphone NA NA
7 Before_sleep_TV NA NA
8 CableTV_No NA NA
9 CableTV_Yes NA NA
10 cafe_No NA NA
# ... with 150 more rows