R中归一化数据时出现“undefined columns selected”错误求助
nr.employed Normalization in R Hey there, let's break down why you're hitting that frustrating "undefined columns selected" error when normalizing nr.employed—even though you've confirmed it's numeric and has no missing values. This error almost always traces back to column name mismatches or indexing mistakes, so let's walk through the most likely fixes:
1. Double-Check Column Name Exact Match
First, make sure the column name in your code perfectly matches what's in the dataset. Tiny details like typos, hidden spaces, or capitalization differences can throw this error.
Run this code to verify if nr.employed actually exists in your data frame:
# Check if the column exists in the data frame any(colnames(markting_train_dim_deleted) == "nr.employed") # Print all column names to spot discrepancies colnames(markting_train_dim_deleted)
If the result is FALSE, you might have a typo (e.g., nr_employed instead of nr.employed) or hidden whitespace. Fix the column name in your code to match exactly.
2. Fix Indexing Syntax
If you're using bracket notation ([, ]) to select the column, forgetting quotes around the column name will cause R to treat nr.employed as a variable instead of a column label.
Wrong Code:
# Missing quotes around column name—R looks for a variable named nr.employed markting_train_dim_deleted$nr.employed_norm <- scale(markting_train_dim_deleted[, nr.employed])
Correct Code:
Use either quoted column names with brackets, or the safer $ notation (which avoids this issue entirely):
# Option 1: Quoted column name with brackets markting_train_dim_deleted$nr.employed_norm <- scale(markting_train_dim_deleted[, "nr.employed"]) # Option 2: $ notation (most straightforward and error-resistant) markting_train_dim_deleted$nr.employed_norm <- scale(markting_train_dim_deleted$nr.employed)
3. Remove Hidden Whitespace from Column Names
Sometimes column names have invisible leading/trailing spaces that you can't spot at a glance. Clean up all column names with this code:
# Trim whitespace from all column names colnames(markting_train_dim_deleted) <- trimws(colnames(markting_train_dim_deleted))
Then try your normalization code again.
4. Confirm the Column Wasn't Accidentally Dropped
When you processed euribor3m, it's possible you used a function like select() or subset() that inadvertently removed nr.employed. Check the data frame structure to confirm the column is still present:
# Inspect the full structure of your data frame str(markting_train_dim_deleted)
Look for nr.employed in the output to confirm it's still a numeric column in the dataset.
内容的提问来源于stack exchange,提问作者Rohit Haritash

