变更纬度Lon向量到R中的矩阵 [英] Change Lat & Lon vectors to matrix in R

查看:81
本文介绍了变更纬度Lon向量到R中的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有一些工作, 假设我有以下列数据

I have some work in R, Suppose i have following column data

Lat Lon Var1 Var2 Var3

具有大量模式,由于数据是矢量,因此我想将数据更改为网格矩阵.例如

with the huge number of patterns, since the data are vector i want to change that data into grid matrix. for example

    Lon Lon Lon
Lat Var1 Var1 Var1
Lat Var1 Var1 Var1
Lat Var1 Var1 Var1

R中最有效的命令是什么,谢谢

what is the most effective command in R, thanks

推荐答案

您还可以使用reshape2

library(reshape2)
acast(df, Lat~Lon, value.var='Var1')

如果要更改水平/垂直位置

If you want to change the horizontal/vertical positions

m1 <- acast(df, Lon~Lat, value.var='Var1')
dimnames(m1) <- list(rep('Lon', 3), rep('Lat',3))

使用@josiber的示例,m1应该是

Using @josiber's example, m1 would be

m1
#    Lat Lat Lat
#Lon   1   4   7
#Lon   2   5   8
#Lon   3   6   9

其他选择是使用base R中的xtabs

xtabs(Var1~Lon+Lat, df)
#   Lat
#Lon 1 2 3
#  1 1 4 7
#  2 2 5 8
#  3 3 6 9

假设您需要所有Var's

acast(melt(df, id.var=c('Lat', 'Lon')), Lon~Lat+variable)

这篇关于变更纬度Lon向量到R中的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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