0

我有一个数据集电影。

head(Movies)

头部输出(电影)

如何获取 MovieID 为“0000008”的行?我努力了:

t1 = subset(Movies, "MovieID" == "0000008")
t2 <- Movies[ which(Movies["MovieID"]=="0000008"), ]
head(t1)
head(t2)

两者都返回空数据集,这是错误的,因为我可以看到 ID 为“0000008”的行。

编辑:我尝试从 MovieID 中删除“”,但这会引发错误:

子集.matrix(电影,MovieID ==“0000008”)中的错误:找不到对象'MovieID'

编辑:电影数据是这样获得的:

URL = "https://raw.githubusercontent.com/sidooms/MovieTweetings/master/latest/movies.dat"
MovieText = readLines( remote.file(URL) ) # HACK!!!
Movies = matrix( sapply( MovieText,
            function(x) unlist(strsplit(sub(" [(]([0-9]+)[)]", "::\\1",x),"::"))[1:4] ),
            nrow=length(MovieText), ncol=4, byrow=TRUE )
colnames(Movies) = c("MovieID", "MovieTitle", "Year", "Genres")
4

1 回答 1

2

nrow应该是length(MovieText)/4

URL = "https://raw.githubusercontent.com/sidooms/MovieTweetings/master/latest/movies.dat"
MovieText = readLines( URL ) # HACK!!!
Movies = matrix( sapply( MovieText,
    function(x) unlist(strsplit(sub(" [(]([0-9]+)[)]", "::\\1",x),"::"))[1:4] ),
    nrow=length(MovieText)/4, ncol=4, byrow=TRUE )
colnames(Movies) = c("MovieID", "MovieTitle", "Year", "Genres")

#if you want to work with matrix, then use this
subset(Movies, Movies[,"MovieID"]=="0000008")

编辑:data.framedata.table子集

library(data.table)

MoviesDF <- data.frame(Movies)
MoviesDT <- data.table(Movies)

MoviesDF[MoviesDF["MovieID"] == "0000008", ]
MoviesDT[MovieID == "0000008", ]

顺便说一句:喜欢黑客!!!评论。

于 2016-04-19T08:26:33.067 回答