user2743
user2743

Reputation: 1513

Using to R to check all the columns of a dataset and the columns data is categorical convert to numeric

I'm using R and I have a dataset with about 450 columns and I'm trying to figure out how to loop through all the columns and then if that column's values are categorical then recode that column's values.

attach(my_data)
for(i in names(my_data)){
    # how to check the data of each column
    my_data[[my_data[[i]]]] <- as.numeric(my_data[[i]])
}

That's what I've been able to work out so far, but I'm not sure how to check the data of each column.

Upvotes: 0

Views: 150

Answers (2)

bgoldst
bgoldst

Reputation: 35314

You should precompute which columns are factors are then iterate through only those columns:

str(my_data);
## 'data.frame': 3 obs. of  4 variables:
##  $ V1: int  1 2 3
##  $ V2: Factor w/ 3 levels "4","5","6": 1 2 3
##  $ V3: chr  "a" "b" "c"
##  $ V4: Factor w/ 3 levels "7","8","9": 1 2 3
for (i in which(sapply(my_data,is.factor)))
    my_data[[i]] <- as.numeric(as.character(my_data[[i]]));
str(my_data);
## 'data.frame': 3 obs. of  4 variables:
##  $ V1: int  1 2 3
##  $ V2: num  4 5 6
##  $ V3: chr  "a" "b" "c"
##  $ V4: num  7 8 9

Data

my_data <- data.frame(V1=1:3,V2=factor(4:6),V3=letters[1:3],V4=factor(7:9),stringsAsFactors=F
);

Upvotes: 1

akrun
akrun

Reputation: 887028

We can also do with lapply

my_data[] <- lapply(my_data, function(x) if(is.factor(x))
                       as.numeric(as.character(x)) else x)

Upvotes: 2

Related Questions