0
votes

G$name - character column of dataframe

labels_name <- unique(G$name)

LabelEncoder <- function(value, labels) {
   r <- as.numeric(which(labels == value))
   returnValue(r)
}

G$name1 <- LabelEncoder(G$name, labels_name)

Execution of code above generate next error:

Error in $<-.data.frame(*tmp*, "name1", value = c(1, 4, 7, 10, 11, : replacement has 100650 rows, data has 302194 In addition: Warning message: In labels == value : longer object length is not a multiple of shorter object length

What's wrong?

1

1 Answers

1
votes

I solve my problem by the next way:

Because G$name is a vector I used next additional function

LabelEncoder1 <- function(x, labels) {
    r <- sapply(x, LabelEncoder, labels)
}

G$name1 <- LabelEncoder1(G$name, labels_name)