简体   繁体   中英

R applying function on a dataframe

I am trying to apply this function:

if.class <- function(data){
  as.data.frame(
  if (data == '[1, 4)')   '1'
  else if (data == '[4, 6)')  '2'
  else '3'
)
}

on a entire data frame in order to transform the factor levels [1, 4) and [4, 6) to 1 or 2 or 3. The dataframe looks like this:

> dim(mnm.predict.test.class)
  [1] 5750    1
  > head(mnm.predict.test.class)
   predict(mnm, newdata = testing.logist, type = "class")
  1                                                 [1, 4)
  2                                                 [1, 4)
  3                                                 [1, 4)
  4                                                 [1, 4)
  5                                                 [1, 4)
  6                                                 [1, 4)

I am using this line for the transformation:

 mnm.predict.test.class.factors <- apply(mnm.predict.test.class,c(1,2),if.class)

However, the results is weird:

 head(mnm.predict.test.class.factors)
 predict(mnm, newdata = testing.logist, type = "class")
 [1,] List,1                                                
 [2,] List,1                                                
 [3,] List,1                                                
 [4,] List,1                                                
 [5,] List,1                                                
 [6,] List,1   

any ideas why the transformation is not working as expected ?

You can use the levels function to alter the levels of a factor . For example, if you have the factor variable foo

foo <- factor(
  rep(c("[1, 4)","[4, 6)","[6, 7)","[7, 9)"),2))
R> foo
[1] [1, 4) [4, 6) [6, 7) [7, 9) [1, 4) [4, 6) [6, 7) [7, 9)
Levels: [1, 4) [4, 6) [6, 7) [7, 9)

you can change the levels like this

levels(foo) <- c("1","2","3","3")
R> foo
[1] 1 2 3 3 1 2 3 3
Levels: 1 2 3

In your case, you have a 1 column data.frame , so it would be something like

Df <- data.frame(
  foo = factor(
    rep(c("[1, 4)","[4, 6)",
          "[6, 7)","[7, 9)"),2)))
##
levels(Df[,1]) <- c("1","2","3","3")
R> str(Df)
'data.frame':   8 obs. of  1 variable:
 $ foo: Factor w/ 3 levels "1","2","3": 1 2 3 3 1 2 3 3

And just as a side note, judging by the output of head(mnm.predict.test.class.factors) in your question, it looks like your one column has the unwieldy name predict(mnm, newdata = testing.logist, type = "class") - you might want to change this to something more reasonable to type ( names(mnm.predict.test.class.factors)[1] <- "myVar" for example).

apply returns an array and thus your output. Convert it to a data.frame and you ll be fine:

#example data
df <- data.frame(a=rep('[1, 4)',50) )

> df
        a
1  [1, 4)
2  [1, 4)
3  [1, 4)
4  [1, 4)
5  [1, 4)
6  [1, 4)
7  [1, 4)
8  [1, 4)
9  [1, 4)

#just use your function as you used it but wrapped inside a data.frame function
df2 <-  data.frame(apply(df,c(1,2),if.class))

> df2
   a
1  1
2  1
3  1
4  1
5  1
6  1
7  1
8  1
9  1

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