lapplyã使ãã¨ãã®è±ç¥è
ã¾ãdplyrã§mutate_each使ããã£ã¦è©±ã«ãªããããããªãããå¤é¨ããã±ã¼ã¸ãèªã¿è¾¼ãã®ãããã©ãããããã¯è¬ç¾©ãªã©ã§åå¿è
ã«æããã®ãããã©ãã¨ãã«lapplyã使ããªããã°ãããªãããããªå ´é¢ãããã¨æãã¾ãã
ããããlapplyã®çµæã¯ãªã¹ãã§è¿ã£ã¦ããã
> smp <- data.frame(v1=letters[1:5], v2=LETTERS[1:5],stringsAsFactors = FALSE) > smp2 <- lapply(smp, function(x)iconv(x, from="CP932", to="UTF-8")) > smp2 $v1 [1] "a" "b" "c" "d" "e" $v2 [1] "A" "B" "C" "D" "E"
ããã§æ·»ãåãæå®ããªã[]ã使ãã¨ããªã¹ãã«ãªããªãã
> smp3 <- smp > smp3[] <- lapply(smp3, function(x)iconv(x, from="CP932", to="UTF-8")) > smp3 v1 v2 1 a A 2 b B 3 c C 4 d D 5 e E
ãããªãã¨ã«ãªãçç±ã¯ã[<-ã®ã³ã¼ããè¦ãã¨ãããã
[<-ã®ä¸ã§ã¯éä¸ã§ä»å¤å¯¾è±¡xã®ã¯ã©ã¹ãåå¾ããæå¾ã®æ¹ã§è¿ãå¤ã«ãã®ã¯ã©ã¹ãä»ä¸ãã¦ããã
ã¤ã¾ãã代å
¥å¯¾è±¡ã®ãªãã¸ã§ã¯ãã®ã¯ã©ã¹ãä¿åãã¦ããã
cl <- oldClass(x) # ä¸ç¥ class(x) <- cl
ä¸æ¹ãé常ã®ä»å¤ã®å ´åã¯ä»£å
¥ããå¤ã®ã¯ã©ã¹ãç¨ããããã
ãããã£ã¦ãä¸è¨ã³ã¼ãã®smp2ã§ã¯lapplyã®çµæè¿ã£ã¦ããããªã¹ããããã®ã¾ã¾smp2ã«ä»å¤ãããã®ã«å¯¾ããsmp3ã§ã¯lapplyã®çµæã§ãããªã¹ãã«ä»å¤å¯¾è±¡ã®ã¯ã©ã¹ã§ããdata.frameãä»ä¸ããããããsmp3ã¯ãã¼ã¿ãã¬ã¼ã ã¨ãªã£ã¦ããã
ã¨ãããã¾ã§æ¸ãã¦æã£ããã©ã以ä¸ã®ããã«data.frameé¢æ°ã§æ示çã«å¤æããæ¹ãããããããã§ãããã俺ã¯ä½ãã ãã ãæ¸ãã¦ããã ã
data.frame(lapply(smp3, function(x)iconv(x, from="CP932", to="UTF-8")), stringsAsFactors=FALSE)
ã¾ãã誰ãã®ã³ã¼ãã§æ·»åæå®ã®ç¡ã[]ãåºã¦ããã¨ãã®è±ç¥èã¨ãã¦ç¥ã£ã¦ããã¨è¯ããããããªãã