File: unfactor.R

package info (click to toggle)
r-bioc-genelendatabase 1.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 103,600 kB
  • sloc: makefile: 2
file content (26 lines) | stat: -rw-r--r-- 598 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
unfactor=function(var){
	if (is.factor(var)){
		tmp=names(var)
		tmpopt=getOption("warn")
		options(warn=-1)
		out=as.numeric(levels(var))[as.integer(var)]
		options(warn=tmpopt)
		if(any(is.na(out)) & any(is.na(out)!=is.na(var))){
			out=as.character(levels(var))[as.integer(var)]
		}
		names(out)=tmp
	}else if(is.data.frame(var)){
		#Have to use a loop, since calling apply will return a matrix
		out=var
		for(i in 1:dim(var)[2]){
			out[,i]=unfactor(var[,i])
		}
	}else if(is.list(var)){
		#Mmmmm, recursion
		out=lapply(var,unfactor)
	}else{
		#The default option
		out=var
	}
	return(out)
}