Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
# Creating a data frame df <- data.frame( name = c("Alice", "Bob", "Carol", "Dave", "Eve"), age = c(28, 34, 22, 41, 30), salary = c(55000, 72000, 48000, 95000, 61000), active = c(TRUE, TRUE, FALSE, TRUE, FALSE), dept = factor(c("HR", "IT", "IT", "Finance", "HR")) ) # Structure and summary str(df) # compact display of types and first values summary(df) # descriptive stats per column dim(df) # 5 5 nrow(df) # 5 ncol(df) # 5 colnames(df) # "name" "age" "salary" "active" "dept" rownames(df) # "1" "2" "3" "4" "5" # First / last rows head(df, 3) tail(df, 2) # Adding a new column df$bonus <- df$salary * 0.1 # Modifying a column in place df$age <- df$age + 1 # everyone ages one year # Reading / writing CSV (paths are illustrative) # df2 <- read.csv("employees.csv", stringsAsFactors = FALSE) # write.csv(df, "employees_out.csv", row.names = FALSE) # Check for missing values per column colSums(is.na(df)) # Quick frequency table for a factor column table(df$dept)
Result
Open