我正在尝试进行预处理,并希望将classe
因子值{A,B,C,D,E}
转换为{1,2,3,4,5}
。
classe
列的类型为factor
,我提供了所有步骤,如下所示:
#get the data
training <- read.table("http://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv",header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
training_df <- data.frame(training,stringsAsFactors=FALSE)
#split to training & test sets
inTrain <- createDataPartition(y=training$classe, p=0.75, list=FALSE)
training_data <- training[inTrain,]
testing_data <- training[-inTrain,]
#subset based on columns of interest, based on previous studies
training_data_subset <- subset(training_data, select=c("avg_roll_belt","var_roll_belt","var_total_accel_belt","amplitude_roll_belt","max_roll_belt","var_roll_belt",
"var_accel_arm","magnet_arm_x","magnet_arm_y","magnet_arm_z","accel_dumbbell_y","accel_dumbbell_z","magnet_dumbbell_x","gyros_dumbbell_x",
"gyros_dumbbell_y","gyros_dumbbell_z","pitch_forearm","gyros_forearm_x","gyros_forearm_y","classe"))
#see which columns are factors, the training_data_subset#classe feature is a factor
sapply(training_data_subset, class)
#sapply output
avg_roll_belt var_roll_belt var_total_accel_belt amplitude_roll_belt max_roll_belt
"numeric" "numeric" "numeric" "numeric" "numeric"
var_roll_belt.1 var_accel_arm magnet_arm_x magnet_arm_y magnet_arm_z
"numeric" "numeric" "integer" "integer" "integer"
accel_dumbbell_y accel_dumbbell_z magnet_dumbbell_x gyros_dumbbell_x gyros_dumbbell_y
"integer" "integer" "integer" "numeric" "numeric"
gyros_dumbbell_z pitch_forearm gyros_forearm_x gyros_forearm_y classe
"numeric" "numeric" "numeric" "numeric" "factor"
我创建了一个试图替换A = 1,B = 2,C = 3,D = 4,E = 5的函数,见下文:
factorsToNumeric <- function(data)
{
data_numeric <- data
data_numeric$classe <-as.numeric(factor(toupper(as.character(data_numeric$classe))))
#loop through the data frame based on replace values
for(i in 1:nrow(data_numeric))
{
if ((data_numeric[i,]$classe == "A") || (data_numeric[i,]$classe == "a"))
{data_numeric[i,]$classe <- "1"}
else if ((data_numeric[i,]$classe == "B") || (data_numeric[i,]$classe == "b"))
{data_numeric[i,]$classe <- "2"}
else if ((data_numeric[i,]$classe == "C") || (data_numeric[i,]$classe == "c"))
{data_numeric[i,]$classe <- "3"}
else if ((data_numeric[i,]$classe == "D") || (data_numeric[i,]$classe == "d"))
{data_numeric[i,]$classe <- "4"}
else if ((data_numeric[i,]$classe == "E") || (data_numeric[i,]$classe == "e"))
{data_numeric[i,]$classe <- "5"}
else
{
#do nothing
}
}
return (data_numeric)
}
然而,我收到此错误:
training_data_subset_numeric <- factorsToNumeric(training_data_subset)
错误:
Warning messages:
1: In `[<-.factor`(`*tmp*`, iseq, value = "1") :
invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, iseq, value = "1") :
invalid factor level, NA generated
3: In `[<-.factor`(`*tmp*`, iseq, value = "1") :
invalid factor level, NA generated
4: In `[<-.factor`(`*tmp*`, iseq, value = "1") :
invalid factor level, NA generated
5: In `[<-.factor`(`*tmp*`, iseq, value = "1") :
invalid factor level, NA generated
6: In `[<-.factor`(`*tmp*`, iseq, value = "1") :
invalid factor level, NA generated
7: In `[<-.factor`(`*tmp*`, iseq, value = "1") :
invalid factor level, NA generated
8: In `[<-.factor`(`*tmp*`, iseq, value = "1") :
invalid factor level, NA generated
9: In `[<-.factor`(`*tmp*`, iseq, value = "1") :
invalid factor level, NA generated
进一步检查显示,该列&#34; classe&#34;的班级被转换为&#34;数字&#34;:
sapply(training_data_subset_numeric, class)
avg_roll_belt var_roll_belt var_total_accel_belt amplitude_roll_belt max_roll_belt
"numeric" "numeric" "numeric" "numeric" "numeric"
var_roll_belt.1 var_accel_arm magnet_arm_x magnet_arm_y magnet_arm_z
"numeric" "numeric" "integer" "integer" "integer"
accel_dumbbell_y accel_dumbbell_z magnet_dumbbell_x gyros_dumbbell_x gyros_dumbbell_y
"integer" "integer" "integer" "numeric" "numeric"
gyros_dumbbell_z pitch_forearm gyros_forearm_x gyros_forearm_y classe
"numeric" "numeric" "numeric" "numeric" "numeric"
然而,头部功能确认了上述错误&amp;所有值A,B,C,D,E都被错误地替换为NA
。
答案 0 :(得分:2)
因素不起作用。您不能像其他数据类型那样使用简单的<-
赋值更改值。您可以通过几种不同的方式更改因素。这是使用levels<-
替换函数的一种方式。
以下是您的大量数据中的示例,这些数据花了太长时间才能阅读:)对于这些数据而言,这很简单,因为这些级别已经按正确的顺序排列。
set.seed(2)
x <- sample(training$classe, 20)
x
# [1] A D C A E E A E B C C A D A B E E A B A
# Levels: A B C D E
levels(x) <- 1:5
x
# [1] 1 4 3 1 5 5 1 5 2 3 3 1 4 1 2 5 5 1 2 1
# Levels: 1 2 3 4 5
所以你不需要你的功能。你可以简单地做
levels(training$classe) <- 1:5
我们可以看到新列的str
显示更改后的值
str(training$classe)
# Factor w/ 5 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
请注意,对于这个简单的情况,as.integer(training$classe)
也有效。虽然大部分时间都不会那么容易。
答案 1 :(得分:0)
如果您要转换classe
的{{1}}列,则无需定义自己的功能。您可以使用training_data_subset
向量:
LETTERS