将CSV文件的第三列重新形成矩阵 [英] Reshaping the third column of a CSV file into a matrix

查看:120
本文介绍了将CSV文件的第三列重新形成矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV文件,我想将前3列重新形成矩阵。
我有一个data.frame看起来像这样。

I have a CSV file and I'd like to reshape the first 3 columns into a matrix. I have a data.frame that looks like this.

x a 1 
x b 2 
x c 3 
y a 3 
y b 3 
y c 2 

形式,所以我可以喂它到热图制作一个情节。结果应如下所示:

I want this in matrix form so I can feed it to heatmap to make a plot. The result should look something like:

    a    b    c
x   1    2    3
y   3    3    2

我已经尝试从重塑包中转换,我已经尝试写一个手动函数来做到这一点,

I have tried cast from the reshape package and I have tried writing a manual function to do this but I do not seem to be able to get it right.

CSV文件非常大(900mB),在第一列中有很多x,y,z可以这么说。

The CSV file is very large (900mB) and in the first column there's a lot of x,y,z's so to speak.

推荐答案

可以使用 dcast fucntion。

It can be done using dcast fucntion.

library(reshape2)
textData <- "x a 1 
x b 2 
x c 3 
y a 3 
y b 3 
y c 2 "

data <- read.table(textConnection(textData), header=FALSE)
dcast(data, V1 ~ V2, value.var="V3")

这篇关于将CSV文件的第三列重新形成矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆