What is the equivalent of var_dump() in R?

≡放荡痞女 提交于 2019-11-30 19:23:43

A few examples:

foo <- data.frame(1:12,12:1) 
foo ## What's inside?
dput(foo) ## Details on the structure, names, and class
str(foo) ## Gives you a quick look at the variable structure

Output on screen:

foo <- data.frame(1:12,12:1)

foo
   X1.12 X12.1
1      1    12
2      2    11
3      3    10
4      4     9
5      5     8
6      6     7
7      7     6
8      8     5
9      9     4
10    10     3
11    11     2
12    12     1

> dput(foo)

structure(list(X1.12 = 1:12, X12.1 = c(12L, 11L, 10L, 9L, 8L, 
7L, 6L, 5L, 4L, 3L, 2L, 1L)), .Names = c("X1.12", "X12.1"), row.names = c(NA, 
-12L), class = "data.frame")

> str(foo)

'data.frame':   12 obs. of  2 variables:
 $ X1.12: int  1 2 3 4 5 6 7 8 9 10 ...
 $ X12.1: int  12 11 10 9 8 7 6 5 4 3 ...

Check out the dump command:

> x <- c(8,6,7,5,3,0,9)
> dump("x", "")
x <-
c(8, 6, 7, 5, 3, 0, 9)

I think you want 'str' which tells you the structure of an r object.

Try deparse, for example:

> deparse(1:3)
[1] "1:3"
> deparse(c(5,6))
[1] "c(5, 6)"
> deparse(data.frame(name=c('jack', 'mike')))
[1] "structure(list(name = structure(1:2, .Label = c(\"jack\", \"mike\""
[2] "), class = \"factor\")), .Names = \"name\", row.names = c(NA, -2L" 
[3] "), class = \"data.frame\")" 

It's better than dump, because dump requires a variable name, and it creates a dump file.

If you don't want to print it directly, but for example put it inside a string with sprintf(fmt, ...) or a variable to use later, then it's better than dput, because dput prints directly.

print is probably the easiest function to use out of the box; most classes provide a customised print. They might not specifically name the type, but will often provide a distinctive form.

Otherwise, you might be able to write custom code to use the class and datatype functions to retrieve the information you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!