library(ISwR) attach(stroke) names(stroke) is.data.frame(stroke) stroke$age # extract a single variable (indexing part of the data frame) stroke[, "age"] # equivalent to the previous one (indexing part of the data frame, array, list) stroke[1:5, 1:3] # extract selected rows and selected column numbers stroke[c(12, 20, 505), ] # extract selected rows and all columns age_ordered <- stroke[order(stroke$age), ] # order the data frame names(age_ordered) head(stroke) stroke[age_ordered$age>80, c("age", "dgn", "coma", "diab")] # extract using the conditional logic age_ordered[age_ordered$age>80, c("age", "dgn", "coma", "diab")] # extract using the conditional logic nrow(stroke) sum(is.na(stroke$diab)) # How to count the number of rows with NA's ('diab' column) in the data frame sum(stroke$diab == 'Yes', na.rm=TRUE) # How to count the number of rows with 'Yes' ("diab" column) in the data frame sum(stroke$diab == 'No', na.rm=TRUE) # How to count the number of rows with 'No' ("diab" column) in the data frame table(stroke$diab) # frequency table table(stroke['diab']) # equivalent to the previous one - how to count the number of occurrences in a column ls() rm(list = ()) ################### library(dplyr) # Create a data frame df1 <- data.frame(sex= c('Male','Female', 'Male', 'Female'), num = c(1,2,3,4) ) df1 # Create a 1/0 variable from a chatracter variable (Male/Female) df1$is_male <- (df1$sex %in% 'Male')*1 # Create a logical variable (RUE/FALSE) from a chatracter variable (Male/Female) df2 <- within(df1, (is_male = sex %in% 'Male')*1 ) # Create a 1/0 variable from a character variable (Male/Female) df3 <- mutate(df1, is_male = (sex %in% 'Male')*1) data.frame(df1$is_male, df2$is_male, df3$is_male) ############# setwd("C:/r-basics/Data") getwd() load ("class_x.rdata") class <-class_x class <- within(class, { gender_fac1 <- factor(sex,levels=c ('M','F'), labels=c('Male', 'Female')) }) class$gender_fac2 <- factor(class$sex, labels=c('Male Student', 'Female Student')) class[order(class$gender_fac1), ] library(Hmisc) contents(class) ########## setwd ("C:/r-basics/Data") load("class_x.rdata") t_class_x <- class_x names(t_class_x) library (Hmisc) contents(t_class_x) library(tidyverse) # Create new variables class <- t_class_x %>% mutate(male_dummy = case_when(sex=="M" ~ 1, sex=="F"~ 0), weight_cat=case_when((weight <= 100) ~ '<=100', (weight > 100) & (weight <= 115) ~ '100-115', (weight > 115) ~ '116+'), female_dummy = if_else(sex=='F', 1,0), age_cat= cut(age, breaks=c(11, 12, 13, 14, 16), labels=c("11-12", "12-13", "14-15", "15-16"), include.lowest=TRUE, stringsAsFactors = FALSE) ) table(class$weight_cat) table(class$male_dummy) table(class$female_dummy) table(class$age_cat) ########### test <- data.frame(vara1=1:10,varb1=2:11,vara2=3:12,varb2=4:13) test test[paste0("varc",1:2)] <- test[paste0("vara",1:2)] + test[paste0("varb",1:2)] test ############ library(ISwR) attach(stroke) some_object <- as.data.frame(table(cut(age, c(1, 29, 49, 64, 96)))) some_object ls() search() rm(list = ls() )