R语言DataFrame无法进行正态性检验的问题求助
Hey there! Let's break down why you can't run normality tests on your DataFrame and get it sorted out quickly.
The Root Problem
Looking at your str(df) output, both trtGroup and cd are stored as Factor types. Normality tests (like Shapiro-Wilk or Kolmogorov-Smirnov) require variables to be numeric—factors (categorical data) can't be tested for normality, which is why you're hitting roadblocks.
How to Fix It
Let's adjust your data processing steps to ensure numeric columns stay (or get converted to) numeric types:
Fix the Initial Data Import & Conversion
Yourread.csvusesstringsAsFactors=FALSE(great call!), but theas.data.frame(lapply(df, unlist))step might be converting character columns back to factors (older R versions default tostringsAsFactors=TRUEinas.data.frame). Let's rewrite that code to preserve numeric types:# Read CSV with explicit settings to avoid unwanted factors df <- read.csv('cdSH2015Fall.csv', dec = ".", na.strings = c("na"), header=TRUE, row.names=NULL, stringsAsFactors=FALSE) # Convert factor/character columns to numeric (skip categorical columns like trtGroup) # First, identify which columns should be numeric (exclude your grouping variable) numeric_columns <- setdiff(names(df), "trtGroup") # Batch convert to numeric, handling factors properly df[numeric_columns] <- lapply(df[numeric_columns], function(col) { # If the column is a factor, convert to character first to avoid level codes if (is.factor(col)) { col <- as.character(col) } as.numeric(col) })Verify the Data Structure
Runstr(df)again—you should see numeric columns (labelednum) instead of factors for all variables you want to test for normality. For example,cdshould now show asnuminstead ofFactor w/ 2 levels "0","1".Run Your Normality Test
Now you can run tests like Shapiro-Wilk on numeric columns. For example, testing thecdcolumn (with NA handling):# Shapiro-Wilk test for normality (omit NA values) shapiro.test(na.omit(df$cd))
Quick Notes
- You don't need to run normality tests on categorical variables like
trtGroup—those tests only apply to continuous numeric data. - If you still see non-numeric values in your columns, double-check your CSV file for stray characters (like spaces or symbols) that might be forcing columns to be read as characters/factors.
内容的提问来源于stack exchange,提问作者Delia Shelton

