[R] data types changing

dmmtchll at uiuc.edu dmmtchll at uiuc.edu
Tue Oct 30 23:27:51 CET 2007

I am attempting to take a row of data from one matrix to another if it satisfies a certain condition.  When R puts the data in the new matrix it is changing the characters to integers and giving them values which aren't very helpful to my analysis.  I would use as.character, but I need to keep the double values as is.  Is there a way to copy the entry exactly as is to the new matrix, without having to use a separate statement for each entry.  I've included some code below.  In my example all but row 5 are factors and should be kept as characters, and column 7 has 2 levels, 1 or 2.  Column 5, the response is a double.  Thanks for the help.

l = nrow(data)
n = 1
k = 1
data1trip = as.data.frame(rbind(1:7))
data2trip = as.data.frame(rbind(1:7))
colnames(data1trip) =...#my column names for data1trip
colnames(data2trip) =...# ""              "" data2trip
for(i in 1:l){
	if (data[i,7]=="1"){
	data1trip[k,] = data[i,]; k=k+1 }
	else {
	data2trip[n,] = data[i,]; n = n+1}

is the only other way to do what i want....

for(i in 1:l){
	if (data[i,7]=="1"){
	data1trip[k,1] = as.char(data[i,1]
        data1trip[k,2] = as.char(data[i,2]
        data1trip[k,3] = data[i,]

        else {
	and the same for those which have a "2" value in data[i,7]?

This second way seems a bit sloppy and i wanted to check if there existed a more "professional" way to do it.

Thanks again,
Dave Mitchell

More information about the R-help mailing list