Reputation: 1291
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
Upvotes: 1
Views: 60
Reputation: 341
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.
Upvotes: 1