Removing character data from numeric dataframe in R -


i have dataframe has header recycled couple of times, looks this:

var1    var2    var3    var4    1       1       1     'ch'    1       1       1     'ch'    1       1       1     'ch' var1    var2    var3    var4    1       1       1     'ch'    1       1       1     'ch'    1       1       1     'ch' var1    var2    var3    var4 

most of variables have numeric values; some, however, have character – converting whole df numeric won't me. wondering how subset dataframe remove re-appearing header? so, have this:

var1    var2    var3    var4    1       1       1     'ch'    1       1       1     'ch'    1       1       1     'ch'    1       1       1     'ch'    1       1       1     'ch'    1       1       1     'ch' 

having headers have turned of data factors (or character if used stringsasfactors=false):

dd <- read.table(text="var1    var2    var3    var4    1       1       1     'ch'    1       1       1     'ch'    1       1       1     'ch' var1    var2    var3    var4    1       1       1     'ch'    1       1       1     'ch'    1       1       1     'ch' var1    var2    var3    var4") 

convert last column numeric (ignore warnings):

dd[,1:3] <- lapply(dd[,1:3],                     function(x) as.numeric(as.character(x))) 

throw away rows first 3 columns na:

dd <- dd[apply(dd[,1:3],1,function(x)!all(is.na(x))),] 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -