Learn R Programming

shipunov (version 1.11)

Str: 'str()' enchanced for data frames

Description

Enhanced 'str()': with variable numbers, row names, missing data indication and possibly more

Usage

Str(df, as.factor=FALSE)

Arguments

df

Data frame

as.factor

Convert character columns to factors?

Value

If 'as.factor=TRUE', invisibly outputs the data frame with all character columns converted into factors.

Details

'Str()' is an enhanced 'str()'. 'Str()' (1) shows data frame structure with column indexes, (2) indicates presence of NA(s) with star (*) and (3) lists first five row names, if they are not default.

If the object is a data frame with atomic columns, this function captures output of internal 'str()', changes it and outputs the new one. If the object is not a data frame or is a data frame with non-atomic columns, then output is not changed.

If 'as.factor=TRUE', converts all character columns to factors before reporting the structure, thus mimicking pre-R4 behavior of many functions related with data frames (and also invisibly outputs the new data frame). Might be useful as is, for example, to understand the number of unique character values which will be shown as "factor levels".

Alternative: DescTools::Str() which uses cycles (slower!), has less features, but works with non-atomic columns.

See Also

str

Examples

Run this code
# NOT RUN {
trees1 <- trees
row.names(trees1)[1] <- "a"
trees1[1, 1] <- NA
Str(trees)
Str(trees1)

# }
# NOT RUN {
trees.crazy <- trees
trees.crazy[[2]] <- trees[, 2, drop=FALSE]
Str(trees.crazy) # does not work with data frames with non-atomic columns
# }
# NOT RUN {
abc <- data.frame(a=letters[1:10], b=LETTERS[1:10], c=1:10)
abc[3, 1] <- NA
Str(abc)
abc.new <- Str(abc, as.factor=TRUE)
Str(abc.new)

# }

Run the code above in your browser using DataLab