How to create a matrix in R using the rows and column data? -
i new r , used refer lot here in stackoverflow. compare each rows , columns , create matrix.
data line_name marker_name genodata generation line_a marker_1 aa f7 line_a marker_2 tt f7 line_a marker_3 cc f7 line_b marker_1 tt f7 line_b marker_3 @ f6 line_b marker_3 aa f7 line_c marker_2 aa f7 line_c marker_2 -- f8 line_d marker_1 -- f7 line_d marker_1 aa f8 line_d marker_4 aa f8
i
[,marker_1] [,marker_2] [,marker_3] [,marker_4] [line_a] aa tt cc -- [line_b] tt -- aa -- [line_c] -- aa -- -- [line_d] aa -- -- aa
please me !
i have edited question additional rules should implemented
rule 1: if line has same marker repeated 2 or more , geno data different, need check generation column , pick latest generation. in example line b, marker_3 has 2 different genodata @ , aa. based on generation aa should override at
rule 2: if if line has same marker repeated 2 or more , geno data missing despite of generation, available data should override --. in example line c, marker_2 has genodata aa , --. aa should override --. similarity line d, marker_1 has genodata -- , aa. aa should override --.
thanks
you can create matrix rownames , colnames in first 2 columns of data, use matrix indexing of matrix fill in data:
m <- matrix(,nrow=length(unique(x[,1])), ncol=length(unique(x[,2]))) rownames(m) <- unique(x[,1]) colnames(m) <- unique(x[,2]) m[as.matrix(x[,1:2])] <- as.character(x[,3]) m marker_1 marker_2 marker_3 marker_4 line_a "aa" "tt" "cc" na line_b "tt" na "aa" na line_c na "aa" na na line_d "aa" na na "aa"
Comments
Post a Comment