【问题标题】:Reformatting array into dataframe将数组重新格式化为数据框
【发布时间】:2019-10-04 18:56:47
【问题描述】:

我有一个数据集,其中包含全球 307 个地点的十年(3653 天的温度)。数据设置为一个数组(示例如下),其中包含 307 个经度和 307 个纬度的 3653 个数据值(温度)。

my.array = array(1:344291597, dim=c(307, 307, 3653))

我想生成一个data.frame,其中包含沿列的 3653 天的温度值,沿行的坐标位于两列中。例如:

Lon     Lat     01/01/1971     02/01/1971     03/01/1971     04/01/1971
20.5    60.5    -6.5           -7.1           -5.9           -6.3
20.5    61.5    -6.4           -7.2           -6.8           -6.5
20.5    62.5    -7.7           -7.9           -7.3           -7.4

有解决办法吗?我尝试了各种方法都没有成功。

【问题讨论】:

  • 您需要matrix(my.array, nrow = 307, ncol = 3653) 吗?
  • 感谢@RonakShah,但这并不能提供所需的结果。我需要数据框包含数据数组中 307 个经度和纬度的 3653 列。
  • 实际上,我不清楚您的预期输出是什么。 my.array 的数字与预期输出中的数字不同。

标签: r


【解决方案1】:

我认为这应该可行

my.array = array(1:344291597, dim=c(307, 307, 3653))
#Find locations from array
Locations <- cbind(my.array[,1,1],my.array[1,,1])
#Create new matrix to store temperature values
LocationsData <- matrix(data= NA,nrow = dim(my.array)[1], ncol = dim(my.array)[3])
#Populate new matrix with temperature values 
for (i in 1:dim(my.array)[1]){

    LocationsData[i,] <-as.vector(t(my.array[i,i,]))
}

Desiredoutput <- data.frame(cbind(Locations,LocationsData))
#add correct names
Names(Desiredoutput) <- c("Lon", "Lat", dimnames(my.array)[[3]])

【讨论】:

    猜你喜欢
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 2019-05-19
    • 2021-11-09
    相关资源
    最近更新 更多