使用 R 将数据帧的多行转换为具有多列的一行有多、转换为、数据

由网友(〆視嬿丶洳掵)分享简介:我有一个四列的数据框:I have a data frame with four columns : df=data.frame( UserId=c(1,2,2,2,3,3), CatoId=c('C','A','B','C','D','E'), No=c(1,9,2,2,5,3)) UserId CatoId...

我有一个四列的数据框:

I have a data frame with four columns :

df=data.frame( UserId=c(1,2,2,2,3,3), CatoId=c('C','A','B','C','D','E'), No=c(1,9,2,2,5,3))  

UserId CatoId No  
1       C     1  
2       A     9  
2       B     2  
2       C     2  
3       D     5  
3       E     3

我想将结构转换为以下结构:

I would like to transform the structure into the following one :

UserId A B C D E  
  1    0 0 1 0 0  
  2    9 2 2 0 0 
  3    0 0 0 5 3

列代表 CatoId 中所有可能的值.第一个数据帧有 200 万行,CatoId 有 21 个不同的值.所以我不想使用任何循环.有没有办法用 R 做到这一点.否则最好的方法是什么?我的目标是在最后一个数据帧上应用聚类算法.

Where the columns represents all possible values in CatoId. The first data frame has 2 million rows and CatoId has 21 different values. So I don't want to use any loops. Is there a way to do this with R. Otherwise what is the best way to proceed? My goal would be to apply a clustering algorithm on the last dataframe.

推荐答案

您可以使用 dcast 做到这一点:

You can do this using dcast:

df1 <- dcast(df, UserId ~ CatoId, value.var = "No", fill = 0)
阅读全文

相关推荐

最新文章