表格

时间:2015-11-06 09:55:47

标签: r

我在R中有一组表,我想审核它们。 我想创建一个表,其中包含内存中data.frame的每一列,data.frame和列的类型。

是否有现成的功能? 如果我自己写的话,最好的方法是什么?

由于str()没有返回任何值,我认为summary + lapply

但我没有设法自动查找data.frame列表。 ls()返回一个对象向量,但我不知道如何将is.data.frame应用于字符串。

我的会议:

df1 <- data.frame(t = 1, t2 = "c")
df2 <- data.frame(t1 = TRUE, t4 = "j", stringsAsFactors = F)
df3 <- data.frame(dt = Sys.Date(), dt2 = Sys.time())
u <- "string"

我想要的结果:

audit <- data.frame(table = c(rep(df1, 2), rep(df2, 2), rep(df3, 2)
       , column = c("t", "t2", "t1", "t4", "dt", "dt2")
       , type = c("num", "Factor", "logi", "chr", "Date", "POSIXct"))
       , stringsAsFactors = F)

1 个答案:

答案 0 :(得分:3)

1)这与你想要的相近吗?

# returns a character vector of names of all data.frames in envir
data.frame.names <- function(envir = .GlobalEnv) {
    Filter(function(nam) is.data.frame(get(nam, envir)), ls(envir))
}

# returns character matrix with names, columns and types of data.frame 
#  with name nam in environment envir
data.frame.info <- function(nam, envir = .GlobalEnv) {
    DF <- get(nam, envir)
    cbind(data.frame = nam, 
          columns = names(DF),
          class = sapply(DF, function(col) class(col)[1]))
}

do.call(rbind, lapply(data.frame.names(), data.frame.info))

,并提供:

    data.frame columns class      
t   "df1"      "t"     "numeric"  
t2  "df1"      "t2"    "factor"   
t1  "df2"      "t1"    "logical"  
t4  "df2"      "t4"    "character"
dt  "df3"      "dt"    "Date"     
dt2 "df3"      "dt2"   "POSIXct"  

2)如果您只想在R控制台上显示信息,那么这可能就足够了。它使用上面的data.frame.names

ls.str(pattern = paste(paste0("^", data.frame.names(), "$"), collapse = "|"))

,并提供:

df1 : 'data.frame':     1 obs. of  2 variables:
 $ t : num 1
 $ t2: Factor w/ 1 level "c": 1
df2 : 'data.frame':     1 obs. of  2 variables:
 $ t1: logi TRUE
 $ t4: chr "j"
df3 : 'data.frame':     1 obs. of  2 variables:
 $ dt : Date, format: "2015-11-06"
 $ dt2: POSIXct, format: "2015-11-06 05:14:40"