Skip to content

Instantly share code, notes, and snippets.

@gghatano
Last active August 29, 2015 14:02
Show Gist options
  • Save gghatano/c424837f3b27c1778d76 to your computer and use it in GitHub Desktop.
Save gghatano/c424837f3b27c1778d76 to your computer and use it in GitHub Desktop.
for文絶対殺すマン
# 関数定義
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