factor level 수정 melt (reshape시 발생하는 문제 해결)



data frame에서 각각의 column의 factor level을 확장하는 방법


DF[2:3] <- lapply(DF[2:3], factor, levels=letters)
melt(DF, id.vars="id", factorsAsStrings=F)$value
[1] a b c z y x
Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z

If however we reset the factors to have the same levels and only then melt:

DF[2:3] <- lapply(DF[2:3], factor, levels=letters)
melt(DF, id.vars="id", factorsAsStrings=F)$value


Assert 

# Assert 개발 -- 위코드에 추가함 

assert("A Poisson random number is non-negative", {

  x = rpois(1, 10)

  (x >= 11)

  (x > -1)  # () is optional because it's the last expression

})



'AI > R Basic' 카테고리의 다른 글

R Studio Server Install in Ubuntu Server  (3) 2018.07.04
Jupyter Notebook으로 R 실행  (2) 2018.04.15
Data Transformation: dplyr package  (0) 2018.04.14
두 개 데이터 프레임 병합, 빼기  (0) 2018.03.04
R Factor 검색  (0) 2017.11.13

+ Recent posts