【发布时间】:2016-05-11 04:46:37
【问题描述】:
我正在尝试转换数据类型以正确绘制数据。
在我看来,我的代码有点复杂,而且效果不佳。
因此,我想请教一些更好的代码。
以下是我的代码。
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 和 urc_neg 是我的数据集。
例如,当我键入“is.numeric(pos[3,3])”时,它返回 false,因为它是一个向量。因此需要将其数据类型转换为数值类型。之后,我分配了行号并在第一列添加了标签“pos”。
以下是将向量类型转换为数值类型的代码。 我认为它有点复杂并且是不利的。因为我认为 for 循环中涉及到不必要的过程,包括转换数据类型(data.frame -> character-> numeric)。
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]))
}
无论如何,我得到了我想要的。当我输入“is.numeric(pos_temp_temp[3,3])”时,它返回 TRUE。
但是,我想为这个问题找到更好的解决方案。 我期待着你的回答:D
【问题讨论】:
-
如果您的代码有点复杂并且没有效果,那么这是一个问题。如果您正在寻求帮助,请完善您的问题。