Last active
August 29, 2015 14:02
-
-
Save gghatano/c424837f3b27c1778d76 to your computer and use it in GitHub Desktop.
for文絶対殺すマン
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 関数定義 | |
func = function(hoge){ | |
return(hoge*hoge) | |
} | |
# 1から10までの整数のベクトルに対して関数を適用したい | |
x = 1:10 | |
# 絶対殺す | |
fx = as.numeric() | |
for(N in x){ | |
fx[N] = func(N) | |
} | |
fx | |
# 関数によっては, ベクトルを突っ込めばそのままいける | |
func(x) | |
# 確実にやるならapply | |
sapply(x, func) | |
# 現代的Rコード | |
library(magrittr) | |
library(dplyr) | |
x %>% | |
as.data.frame %>% | |
mutate(fx = func(x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment