简体   繁体   English

R编程:数据帧到矩阵的转换

[英]R Programming: Data Frame to Matrix conversion

I am trying to populate a matrix from a dataframe in R. 我正在尝试从R中的dataframe填充matrix

# A demo Data Frame
d = data.frame(movie=c(1,2,3),user=c(1,3,2),rating=c(1,4,2))
# Initialize Output Matrix
output<-matrix(data=NA,nrow = length(d$movie),ncol = length(d$user),byrow = FALSE,)


#Function 
# x= movie, y = user, input=data frame to be indexed, out = output matrix
getMatrixFilled<-function(x,y,input,out){
      out[x,y]<-input[x,y];
      out
    }

I want to take movie , user and the corresponding rating and place it matrix 我想拍摄movieuser和相应的rating并将其放置在矩阵中

as

 output[movie,user]<-rating

Thanks for help in advance!! 预先感谢您的帮助!

Input 输入

> d
  movie user rating
1     1    1      1
2     2    3      4
3     3    2      2

Desired Output 期望的输出

> outputM
     [,1] [,2] [,3]
[1,]    1   NA   NA
[2,]   NA   NA    4
[3,]   NA    2   NA

You could try 你可以试试

output[as.matrix(d[1:2])] <- d$rating
output
#     [,1] [,2] [,3]
#[1,]    1   NA   NA
#[2,]   NA   NA    4
#[3,]   NA    2   NA

I suspect you want a simple reshape: 我怀疑你想要一个简单的重塑:

library(reshape2)
acast(d, movie ~ user)
#   1  2  3
#1  1 NA NA
#2 NA NA  4
#3 NA  2 NA

Since we're guessing on the desired output, here are a couple of other alternatives that work similarly to the existing answers: 由于我们在猜测所需的输出,因此这里有一些其他选择与现有答案类似:

xtabs(rating ~ movie + user, d)
#      user
# movie 1 2 3
#     1 1 0 0
#     2 0 0 4
#     3 0 2 0

library(tidyr)
spread(d, user, rating)
#   movie  1  2  3
# 1     1  1 NA NA
# 2     2 NA NA  4
# 3     3 NA  2 NA

And (by popular demand), base R's reshape (but the columns are sorted differently)... 并且(根据普遍需求),基础R会reshape (但列的排序方式有所不同)...

reshape(d, direction = "wide", idvar = "movie", timevar = "user")
#   movie rating.1 rating.3 rating.2
# 1     1        1       NA       NA
# 2     2       NA        4       NA
# 3     3       NA       NA        2

I think @akrun showed you the simplest way. 我认为@akrun向您展示了最简单的方法。 (You were almost there. but the indices needed to be delivered to "[" in the form of a matrix.) I'm posting the application of using a two-column matrix for indexing assignment into matrix locations to the format of the function you requested. (您几乎在那里。但是索引需要以矩阵的形式传递到"[" 。)我正在发布使用两列矩阵将索引分配到函数格式的矩阵位置的应用程序您要求的。

 d = data.frame(movie=c(1,2,3),user=c(1,3,2),rating=c(1,4,2))

 output<-matrix(data=NA,nrow = length(d$movie),ncol = length(d$user) )
 getMatrixFilled<-function(x,y,input,out){
       out[cbind(x,y)] <- input[ , "rating"]
       out
     }
 getMatrixFilled(x=d$movie, y=d$user , input=d, out=output)
#--------------
     [,1] [,2] [,3]
[1,]    1   NA   NA
[2,]   NA   NA    4
[3,]   NA    2   NA

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

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