Created
July 12, 2012 02:27
-
-
Save u110/3095273 to your computer and use it in GitHub Desktop.
大きさの違う行列の結合関数+よく使う処理メモ
This file contains 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
add_zero_mtx<-function(df1,num,side=1){ | |
if(side==1){ | |
return(t(cbind(t(df1),data.frame(matrix(0,ncol=num))))) | |
}else{ | |
return(t(cbind(data.frame(matrix(0,ncol=num)),t(df1)))) | |
} | |
} | |
my_cbind<-function(df1,df2){ | |
num<-abs(nrow(df1)-nrow(df2)) | |
side<-1 # 最後にゼロ行列を追加 | |
# side<-2 # 先頭にゼロ行列を追加 | |
if (nrow(df1)>nrow(df2)){ | |
return(cbind(df1, add_zero_mtx(df2,num,side))) | |
} | |
else if(nrow(df1)==nrow(df2)){ | |
return(cbind(df1, df2)) | |
} | |
else{ | |
return(cbind(add_zero_mtx(df1,num,side), df2)) | |
} | |
} | |
col.d<-intersect(r.d$HOGE,r.d$HOGE) # カラムごとに分割するために重複を取り除く | |
joinfunc <- function(d,col) | |
{ | |
sub.d<-subset(d,d$HOGE==col[1]) | |
i<-2 | |
while(i < length(col)){ | |
sub.d<-my_cbind(sub.d,subset(d,d$HOGE==col[i]) ) | |
i<-i+1 | |
} | |
return(sub.d) | |
} | |
rr.d<-joinfunc(r.d,col.d) | |
rrr.d<-t(rr.d) # 転置 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment