Recently Published
Document
Lab
ICA9_STT810
This is the reference notebook for the ICA9 assignment of STT810 course
Data Frames
I explored the versatility of data frames in R, focusing on their ability to handle multiple data types within a single structure. I began by creating empty and populated data frames with numeric and character columns, then examined their dimensions using nrow(), ncol(), and dim(). I also practiced converting matrices to data frames with as.data.frame(), which offers flexibility in column types. Subsetting data frames using numeric, logical indexing, and column names proved essential for filtering and managing data efficiently. I further enhanced my skills by using subset(), transform(), and within() functions for streamlined data manipulation. Lastly, I worked on converting all columns or selectively converting factor columns to characters, a useful practice for data consistency in merging or exporting tasks.
Plot
# Fit the Kaplan-Meier model, stratifying by 'sex'
> km_fit <- survfit(surv_object ~ Smoking_Status, data = clinical_survival_data)
>
> View(km_fit)
>
> # Plot the Kaplan-Meier survival curve
> ggsurvplot(km_fit, data = clinical_survival_data,
+ pval = TRUE, # Add p-value for log-rank test
+ conf.int = TRUE, # Show confidence intervals
+ xlab = "Time in Days", # Label for x-axis
+ ylab = "Survival Probability", # Label for y-axis
+ legend.title = "Smoking Status", # Title for the legend
+ legend.labs = c("Nonsmoker", "Smoker"), # Labels for each group
+ risk.table = TRUE, # Show risk table below the plot
+ risk.table.height = 0.25, # Adjust the height of the risk table
+ ggtheme = theme_minimal()) # Use a minimal theme
>
>
Problem Set 2
Problem Set 2
The Logical Class
the logical class in R, examining how logical operators handle different conditions and scenarios. I began by defining two numeric values, a and b, and used logical operators || and && to construct conditional statements. These operators efficiently evaluate expressions, short-circuiting when the result is determined by the first condition.
Next, I explored coercion by converting a numeric value to a logical value using as.logical(). This demonstrated how non-zero numeric values are interpreted as TRUE. Finally, I investigated the behavior of logical operations involving NA. The results highlighted how NA propagates uncertainty, returning NA when the outcome is ambiguous, but yielding a definitive result when combined with FALSE.