如何在dataframe中插入实际值作为R中的列?

如何在dataframe中插入实际值作为R中的列?,r,dataframe,R,Dataframe,我知道一些基本的R,但我被这个数据帧处理卡住了。 我需要能够使用R中的包或基函数来转换这样的数据帧 id value variable 1 25.5 max_temp 1 16.4 min_temp 2 23.1 max_temp 3 12.1 min_temp 为此: id max_temp min_temp 1 25.5 16.4 2 23.1 NA 3 N

我知道一些基本的R,但我被这个数据帧处理卡住了。 我需要能够使用R中的包或基函数来转换这样的数据帧

id   value    variable 
1    25.5     max_temp
1    16.4     min_temp
2    23.1     max_temp 
3    12.1     min_temp
为此:

id   max_temp    min_temp
1     25.5        16.4
2     23.1         NA
3     NA           12.1
检查NA示例,因为某些观测缺少测量值。 实际上,我可以直接在excel文件中修复它,但我正在尝试减少手动预处理


谢谢。

您要做的是重塑数据。有很多方法可以做到这一点。这里有一个:

reshape(x, direction='wide', idvar='id', timevar='variable')
  id value.max_temp value.min_temp
1  1           25.5           16.4
3  2           23.1             NA
4  3             NA           12.1
reformae2::dcast
具有更好的语法:

> dcast(x, id ~ variable)
  id max_temp min_temp
1  1     25.5     16.4
2  2     23.1       NA
3  3       NA     12.1

谢谢,事实上第一个对我来说很好。第二个解决方案将值作为具有列名的字符串插入。