mardi 19 novembre 2019

R - Looping over a data set, using ifelse to check the value of a column in order to set a new column (factor)

I'm new to R, I've had this issue for a week now and have attempted many searches for a solution and just can't figure this out.

I am using the House Regression data set from Kaggle and attempting to do some feature engineering. See link below for more information about the data set https://www.kaggle.com/c/house-prices-advanced-regression-techniques

In short, there are two columns: Condition1 and Condition2. Each has 9 factor levels of the same factors. 4/9 factors specify that the house is near a railroad. Instead of using 18 factors to specify if the house is near a railroad, I'm attempting to create a new column: ByRR. This column is a binary column, 0 if the value in either of these two columns is in the RR_List list, 1 if it is.

I've done a series of different methods to try to accomplish this task. The latest is running an lapply function over the data set. I get the following warning message and all of the values are set to 0. I know that there are values that should be = 1.:

'''

    RR_List = c("RRNn","RRAn","RRNe", "RRAe")

    data <-data[c("Condition1","Condition2")]

    data$ByRR = factor(x="No",levels= c("Yes","No"), labels=c(1,0))  

    lapply(data,function(x) {
       x$ByRR <- ifelse(data$Condition1 %in% RR_List ||
               data$Condition2 %in% RR_List, 1, 0)
    })

    Warning messages:
     1: In x$ByRR <- ifelse(data$Condition1 %in% RR_List || data$Condition2 %in%  
       Coercing LHS to a list
     2: In x$ByRR <- ifelse(data$Condition1 %in% RR_List || data$Condition2 %in%  :
       Coercing LHS to a list
     3: In x$ByRR <- ifelse(data$Condition1 %in% RR_List || data$Condition2 %in%  :
       Coercing LHS to a list

'''

Any help with this is much appreciated!

Aucun commentaire:

Enregistrer un commentaire