R中的数据帧到矩阵

时间:2014-05-01 19:34:17

标签: r matrix dataframe

在R中,我使用for循环遍历大数据帧,尝试将第i列第3行中的整数放入另一个矩阵中的特定索引中。特定索引对应于大数据帧中的索引(同样在* i *行中,而是在第2和第4列中)。例如,假设我的数据帧有data_frame [1,2] = 5,data_frame [1,4] = 12,data_frame [1,7] = 375。我想将375放入索引中的矩阵中,其中行的名称为5,列的名称为12。

然而,问题(我认为)是当我执行col_index = which(colnames(matrix)== data_frame [1,2])时,它返回整数0.列名在技术上是5,但我注意到了仅当我执行col_index = which(colnames(matrix)==“5”)时才有效。我怎样才能确保(在我的for循环中)data_frame [i,2]对应于“5”?

数据保存为“out”我想将数据放入的矩阵称为“m”

m=matrix(nrow=87,ncol=87)
fips=sprintf("%03d",seq(1,173,by=2))
colnames(m)=fips
rownames(m)=fips
m[1:40,1:40]

接下来,第3列等于27

的条件
for(i in 8:2446)
{
if(out[i,3]==27)
{
out_col=out[i,4]
out_row=out[i,2]
moves=out[i,7]
col_index=which(colnames(m)==paste(out_col))
row_index=which(rownames(m)==paste(out_row))
m[row_index,col_index]=moves
}
}

很抱歉没有格式化。它将数字放在矩阵中,但它们不是正确的数字,我无法弄清楚什么是错的。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您的示例中存在很多复杂性,但归结为替换mat中的值,其中行名称,列名称和新值存储在out中。让我们从一个可重复的例子开始(如果你发布一个例子会很有帮助!)

# Matrix to have values replaced
mat <- matrix(0, nrow=3, ncol=3)
rownames(mat) <- c("1", "2", "3")
colnames(mat) <- c("4", "5", "6")
mat
#   4 5 6
# 1 0 0 0
# 2 0 0 0
# 3 0 0 0

out <- data.frame(row=c(1, 3, 3), col=c(6, 5, 4), val=c(1, 4, -1))
out
#   row col val
# 1   1   6   1
# 2   3   5   4
# 3   3   4  -1

现在,替换是一个单行:

mat[cbind(as.character(out$row), as.character(out$col))] <- out$val
mat
#    4 5 6
# 1  0 0 1
# 2  0 0 0
# 3 -1 4 0

基本上,我们用{2}矩阵索引mat,其中索引矩阵的每一行都是行名和列名。

在您的示例中,您似乎排除了out的前7行,以及out[,3]不等于27的任何行。您可以简单地基于out进行分组这些要求与realout <- out[out[,3] == 27 & seq(nrow(out)) %in% 8:2446,]类似,然后使用realout进行替换。

请注意,以这种方式进行替换的一个额外好处是,它比在for行中使用out循环要快得多。