r语言 - 用t函数转置数据帧



我需要做一个数据帧的横向(一个大的约3750观察3个变量),但我想这样做:

原始数据帧:

      activity  level           temp
 1    E             A           10-1810
 2    F             B           10-1810
 3    G             B           10-1810
 4    H             C           10-1810
 5    I             C           10-1810
 6    J             D           10-1810
 ..   ...           ...             .. 

我想要得到这个结果:

temp       10-1810  10-1810 10-1810 10-1810 10-1810 ..........
activity   E          F       G        H      I      .........
level      A          B       B        C      C      .........

我的问题是在t函数中,当我做横向时我总是得到一个新的数据帧作为这个

  row.names x1 x2 x3 x4....................
1   temp ...........
2   activity  .......
3   level     ........

我想删除row.names(标题),并使temp数据帧的标题

如果你的dataframe是df

#normal tanspose
newdf = t(df)
#if you want temp as column name
newdf = data.frame(t(df[,c('activity', 'level')]))
colnames(newdf) = df$temp

最新更新