# Create a small exdf object that includes an extra element in addition to the
# required ones (`main_data`, `units`, and `categories`).
small_exdf <- exdf(
data.frame(A = c(3, 2, 7, 9), B = c(4, 5, 1, 8)),
data.frame(A = 'm', B = 's'),
data.frame(A = 'Cat1', B = 'Cat2'),
extra_exdf_element = "This is an example of an extra exdf element"
)
# Accessing elements of `small_exdf`
names(small_exdf) # Get the names of all elements of small_exdf
small_exdf[['units']] # View the units using the `[[` operator
small_exdf$categories # View the categories using the `$` operator
# Accessing elements of `small_exdf$main_data`
small_exdf[,1] # Access the first column
small_exdf[1,] # Access the first row
small_exdf[,'B'] # Access the column named 'B'
small_exdf[1,2] # Access element 1 of column 2
# Equivalent (but longer) commands for accessing elements of `small_exdf$main_data`
small_exdf$main_data[,1] # Access the first column
small_exdf$main_data[1,] # Access the first row
small_exdf$main_data[,'B'] # Access the column named 'B'
small_exdf$main_data[1,2] # Access element 1 of column 2
# Replacing elements of `small_exdf$main_data`
small_exdf[,'A'] <- seq_len(4) # Replace column A with new values
small_exdf[small_exdf[,'A'] > 2, 'B'] <- 0 # Replace some rows of column B with new values
# Creating a new exdf object with a subset of the data from small_exdf. Here we
# specify `return_exdf = TRUE` so that the `[` operator returns an exdf object
# instead of a data frame
new_exdf <- small_exdf[small_exdf[,'A'] > 2, , TRUE]
names(new_exdf) # Check that the `extra_exdf_element` is still present
print(new_exdf) # Check that only the rows with A > 2 are included
Run the code above in your browser using DataLab