简体   繁体   中英

R: recoding 2 variables into 1 inside function

I've run into two problems writing functions for data frames. I often get a data frames with 2 variables, and I want to recode them into one variable.

If V1>0 and V2 <0 then new_variable = "V1>0, V2<0. 

In all of dataframes I've got V1 and V2 have different names.

Problem number 1. I don't know why test_df$newVar, after this function get only "C>0, I>0"

#Using test FUN on example data frame
    test_df.afterFUN <- test_fun(test_df, var1 = "V1", var2 = "V2", newVar = "category")

Problem number 2. Why last argument of this function "newVar" doesn't change name into "category"? If I run the code of this function fitted to single data frame (renaming variable and ect.) it will work and give me what I want (look test_df2)

rm(list = ls())
    library("dplyr") # for filter
    # Preparing example data frame
    rama <- rbind(c(-5:20, -20:5), c(-20:5, -5:20))
    rama <- t(rama)
    colnames(rama) <- c("V1", "V2")
    test_df <- as.data.frame(rama)

#Test FUN

test_fun <- function(df, var1, var2, newVar) {
  df1 <- filter(df, var1 == 0, var2 == 0)
  df1  <- mutate(df1, newVar = "C=0, I=0")
  df2 <- filter(df, var1 == 0, var2  > 0)
  df2  <- mutate(df2, newVar = "C=0, I>0")
  df3 <- filter(df, var1 == 0, var2  < 0)
  df3  <- mutate(df3, newVar =  "C=0, I<0")
  df4 <- filter(df, var1 >  0, var2 == 0)
  df4  <- mutate(df4, newVar =  "C>0, I=0")
  df5 <- filter(df, var1 >  0, var2  > 0)
  df5  <- mutate(df5, newVar =  "C>0, I>0")
  df6 <- filter(df, var1 >  0, var2  < 0)
  df6  <- mutate(df6, newVar =  "C>0, I<0")
  df7 <- filter(df, var1 <  0, var2 == 0)
  df7  <- mutate(df7, newVar =  "C<0, I=0")
  df8 <- filter(df, var1 <  0, var2 >  0)
  df8  <- mutate(df8, newVar =  "C<0, I>0")
  df9 <- filter(df, var1 <  0, var2 <  0)
  df9  <- mutate(df9, newVar =  "C<0, I<0")
   df <- rbind(df1, df2, df3, df4, df5, df6, df7, df8, df9)
   return(df)
    }

    #Using test FUN on example data frame
    test_df.afterFUN <- test_fun(test_df, var1 = "V1", var2 = "V2", newVar = "category")

    # Procedure outside of funcion fitted to test_df
    df1 <- filter(test_df, V1 == 0, V2 == 0)
    df1  <- mutate(df1, newVar = "C=0, I=0")
    df2 <- filter(test_df, V1 == 0, V2  > 0)
    df2  <- mutate(df2, newVar = "C=0, I>0")
    df3 <- filter(test_df, V1 == 0, V2  < 0)
    df3  <- mutate(df3, newVar =  "C=0, I<0")
    df4 <- filter(test_df, V1 >  0, V2 == 0)
    df4  <- mutate(df4, newVar =  "C>0, I=0")
    df5 <- filter(test_df, V1 >  0, V2  > 0)
    df5  <- mutate(df5, newVar =  "C>0, I>0")
    df6 <- filter(test_df, V1 >  0, V2  < 0)
    df6  <- mutate(df6, newVar =  "C>0, I<0")
    df7 <- filter(test_df, V1 <  0, V2 == 0)
    df7  <- mutate(df7, newVar =  "C<0, I=0")
    df8 <- filter(test_df, V1 <  0, V2 >  0)
    df8  <- mutate(df8, newVar =  "C<0, I>0")
    df9 <- filter(test_df, V1 <  0, V2 <  0)
    df9  <- mutate(df9, newVar =  "C<0, I<0")
    test_df2 <- rbind(df1, df2, df3, df4, df5, df6, df7, df8, df9)

This can probably be written nicer, but try:

test_fun <- function(df,col1, col2, newVar) {
    temp <- sapply(df[,c(col1,col2)],function(x) revalue(factor(sign(x)),c("-1"="<0","0"="=0","1"=">0")))
    df[,newVar] <- apply(temp, 1, function(y) paste0(col1,y[1],", ",col2,y[2]))
    df
}

head(test_fun(test_df,"V1", "V2", "category"))
#   V1  V2   category
# 1 -5 -20 V1<0, V2<0
# 2 -4 -19 V1<0, V2<0
# 3 -3 -18 V1<0, V2<0
# 4 -2 -17 V1<0, V2<0
# 5 -1 -16 V1<0, V2<0
# 6  0 -15 V1=0, V2<0

Explanation

We use sign to get the sign of each number within a column (returns -1, 0 or 1). We then rewrite those numbers as the strings "<0","=0" and ">0" using revalue(factor),c()) . We use sapply to apply it to both columns of test_df . This returns a character matrix. We then apply paste to each row to get the character vector that you want. Finally, we assign that vector to test_df$category .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM