Problem

You want to find information about variables.

Solution

Here are some sample variables to work with in the examples below:

x <- 6
n <- 1:4
let <- LETTERS[1:4]
df <- data.frame(n, let)

Information about existence

# List currently defined variables
ls()
#> [1] "df"       "filename" "let"      "n"        "old_dir"  "x"


# Check if a variable named "x" exists
exists("x")
#> [1] TRUE

# Check if "y" exists
exists("y")
#> [1] FALSE


# Delete variable x
rm(x)
x
#> Error in eval(expr, envir, enclos): object 'x' not found

Information about size/structure

# Get information about structure
str(n)
#>  int [1:4] 1 2 3 4

str(df)
#> 'data.frame':	4 obs. of  2 variables:
#>  $ n  : int  1 2 3 4
#>  $ let: Factor w/ 4 levels "A","B","C","D": 1 2 3 4


# Get the length of a vector
length(n)
#> [1] 4

# Length probably doesn't give us what we want here:
length(df)
#> [1] 2

# Number of rows
nrow(df)
#> [1] 4

# Number of columns
ncol(df)
#> [1] 2

# Get rows and columns
dim(df)
#> [1] 4 2