r - How to convert the values in a dataframe to a dummy values by levels() function? -
the given data
snp1 <- c("aa","gg","ag") snp2 <- c("aa","cc","ac") snp3 <- c("gg","aa","ag") df<- data.frame(snp1, snp2, snp3) colnames(df)<- c('rs10000438', 'rs10000500','rs1000055')
i define data function dominant_dummy
. when run codes found goes wrong.
error in if (!check) { : argument of length 0
when debug found argument x
in dataframe
, , need use function levels(x)
check level of x, , assign levels(x)<- c(0,1,1)
, levels
function return null
. my purpose convert values in dataframe df
dummy values based on conditions.
snp_lib<- ncbi_snp_query(names(x)) ncbi_snp_query(names(x)) snp_min<- snp_lib$minor snp_name<- snp_lib$query snp_min ="a" snps <- x check<-substr(levels(snps)[2],1,1)==snp_min
i need assign dummy values dataframe
levels(x)<- c(0,1,1)
. how can that?
library(rsnps) dominant_dummy<- function(x){ snp_lib<- ncbi_snp_query(names(x)) ncbi_snp_query(names(x)) snp_min<- snp_lib$minor snp_name<- snp_lib$query snp_min ="a" snps <- x check<-substr(levels(snps)[2],1,1)==snp_min if(!check){ levels(snps)<-c(0,1,1) snps<-as.numeric(as.character(snp)) }else {levels(snps)<-c(1,1,0) snps<-as.numeric(as.character(snp))} } df_3levels<-sapply(1:ncol(df), function(i) dominant_dummy(df[,i, drop=false]))
you can not check levels on data frame. use levels(snps[[1]])
instead check levels on first column. there other errors well.
wiki
Comments
Post a Comment