简体   繁体   English

更新R中的矩阵元素

[英]Updating matrix elements in R

I'm new to R and have been running into some trouble with updating matrix elements. 我是R的新手,在更新矩阵元素时遇到了一些麻烦。 The code I'm working with is along these lines: 我正在使用的代码遵循以下原则:

library(lubridate)
library(Hmisc)

file=read.csv(filename)
rows<-unique(file$A)
columns<-unique(file$B)
mat<-matrix(0, nrow=length(unique(file$A)), ncol=length(unique(file$B)))
rownames(mat)<-rows
colnames(mat)<-columns

This is a pretty straightforward setup. 这是一个非常简单的设置。 The file has several columns. 该文件有几列。 What I want is to form a matrix of two of the columns. 我想要的是形成两列的矩阵。 One column will serve as the rows of the matrix and the other will serve as the columns. 一列将用作矩阵的行,另一列将用作列。 An example would be the file containing various information about airplane flights and column A refers to origin of the flight while column B refers to destination of the flight. 一个示例是包含有关飞机航班的各种信息的文件,列A指航班的始发地,而列B指航班的目的地。 Thus, the matrix would be a matrix of origin and destinations where each element would be the number of flights from the specific origin to the specific destination. 因此,该矩阵将是始发地和目的地的矩阵,其中每个元素将是从特定始发地到特定目的地的飞行次数。 Since I'm new to R, I tried my approach on a limited part of the original file: 由于我是R语言的新手,所以我在原始文件的有限部分中尝试了这种方法:

for (i in 1:10){
  inc(mat[(file[i,]$A),(file[i,]$B)])<-1
}

The inc function is part of the Hmisc library and it is used to increment variables. inc函数是Hmisc库的一部分,用于递增变量。

When I run the following print statements for example: 例如,当我运行以下打印语句时:

print(mat[(file[2,]$A),(file[2,]$B)])
print(mat["Origin2", "Destination2"])

The first one outputs 1, while the second one outputs 0. I do not understand why this is happening because the two indexes in the print statement refer are the same. 第一个输出1,而第二个输出0。我不明白为什么会这样,因为print语句中引用的两个索引相同。

In addition, I also ran the for loop for all the rows of the file and then wrote the matrix into a csv file. 此外,我还为文件的所有行运行了for循环,然后将矩阵写入了csv文件。 When I examine the csv file, I see that I am getting seemingly arbitrary results. 当我检查csv文件时,看到的是看似随意的结果。 For the most part, the elements are zero but occasionally there are non-zero elements. 在大多数情况下,元素为零,但偶尔会有非零元素。 When I cross-check the non-zero elements with the original file; 当我与原始文件交叉检查非零元素时; however, I get different answers. 但是,我得到了不同的答案。 For instance, one element in the matrix is 38, but when I check the original file there is only 1 entry corresponding to the same origin and destination. 例如,矩阵中的一个元素为38,但是当我检查原始文件时,只有1个条目对应于相同的起点和终点。

Any insight on why this would be happening would be much appreciated. 对于为什么会这样的任何见解将不胜感激。

Try to make your question reproducible. 尝试使您的问题可重复。 I think this could help: 我认为这可以帮助:

origins <- c("A", "B", "C", "D", "A", "B", "A", "B", "E", "D")
dests   <- c("X", "Y", "Z", "X", "U", "V", "X", "T", "Y", "X")

df <- data.frame(origins, dests)
df
#    origins dests
# 1        A     X
# 2        B     Y
# 3        C     Z
# 4        D     X
# 5        A     U
# 6        B     V
# 7        A     X
# 8        B     T
# 9        E     Y
# 10       D     X

table(df$origins, df$dests)
#   T U V X Y Z
# A 0 1 0 2 0 0
# B 1 0 1 0 1 0
# C 0 0 0 0 0 1
# D 0 0 0 2 0 0
# E 0 0 0 0 1 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM