Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想知道是否有办法将 2 列矩阵转换为多图或列表列表。
矩阵的第一列是一个 id(可能有重复的条目),第二列是一些值。
例如,如果我必须遵循矩阵
m <- matrix(c(1,2,1,3,2,4), c(3,2))
我想将其转换为以下列表
[[1]] 3,4 [[2]] 2
使用基本函数,您可以执行以下操作:
tapply(m[,2], m[,1], `[`) # outputs an array by(m, m[,1], function(m) m[,2]) # outputs a by object, which is a list
你可以使用plyr:
plyr
dlply(m, 1, function(m) m[,2]) # outputs a list dlply(m, 1, `[`, 2) # another way to do it...