1

I am trying to convert the data types in order to correctly plot the data.

In my opinion, my codes are a little bit complex and are not effective.

Therefore, I would like to ask some advice for better codes.

Below are my codes.

pos <- as.matrix(read.delim("urc_pos",header=FALSE))
neg <- as.matrix(read.delim("urc_neg",header=FALSE))
rownames(pos) <- 1:nrow(pos)
pos_temp <- cbind("pos",pos[,3:42])
pos_temp_temp <- as.data.frame(pos_temp)
for(i in 2:41)
{
    pos_temp_temp[,i] <- as.numeric(as.character(pos_temp_temp[,i]))
}

urc_pos and urc_neg are my datasets.

For example, when I typed "is.numeric(pos[3,3])", it returned false because it was a vector. Therefore converting its data type to numeric type is necessary. After then, I assigned the row numbers and added label "pos" at the first column.

And below are the codes that convert the vector types to numerical type. I think that it is a little bit complex and is unfavorable. Because I think that unnecessary procedures including converting data types ( data.frame -> character-> numerical) are involved in the for loops.

pos_temp_temp <- as.data.frame(pos_temp)
for(i in 2:41)
{
    pos_temp_temp[,i] <- as.numeric(as.character(pos_temp_temp[,i]))
}

Anyway, I got the what I intended. when I typed "is.numeric(pos_temp_temp[3,3])", it returned TRUE.

However, I want to get the better solutions for this problems. I am looking forward to your answer :D

sclee1
  • 1,095
  • 1
  • 15
  • 36
  • If your codes are a little bit complex and are not effective, then that is one problem. If you are seeking your help, refine your question. – Retro Gamer May 11 '16 at 05:25
  • Please include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). – Jaap May 11 '16 at 07:14

1 Answers1

1

Have you tried the following code:

pos_temp_temp<-as.numeric(as.character(pos_temp))

In general you should try to avoid loops whenever possbile.

Otto_K
  • 347
  • 2
  • 6