English 中文(简体)
数据框架至数组(2列)
原标题:data.frame to array (2 columns)

(d) 想知道如何将数据框架转换成 R. 中的数据阵列。 框架由 16 列和 & gt; 100 行组成。 每行代表一个人( snail) 。 我确实在每只蜗牛上设置了8个地标。 所以现在我已经有了 16 个坐标/ 个体。 没有我想要将数据转换成一个阵列。 设置于此阵列中, 每个人获得自己的矩阵, 所有人都被组合在一个( 大) 阵列中。

我的数据看起来是这样的:6个人,有8个地标 aka 16 坐标( X& Y)

     X1.Lms    Y1.Lms    X2.Lms    Y2.Lms    X3.Lms    Y3.Lms    X4.Lms    Y4.Lms    X5.Lms    Y5.Lms   X6.Lms    Y6.Lms    X7.Lms    Y7.Lms X8.Lms Y8.Lms
1 132500000 132500000 114500000 132000000 150100000 121500000  97600000 123500000 164700000 107800000 77600000 110400000 181600000  81700000     NA     NA
2 135700000 150900000 114100000 152000000 147500000 142500000  96900000 143800000 161900000 128600000 78300000 131200000 178000000 100500000     NA     NA
3 134100000 136900000 113700000 135400000 148700000 129900000  99600000 127800000 164700000 115900000 76600000 115100000 183600000  87600000     NA     NA
4 137400000 147100000 119500000 145600000 149500000 143000000 102400000 136800000 169400000 128200000 84000000 123400000 186700000 106000000     NA     NA
5 141300000 144200000 115800000 141300000 154000000 135800000 103900000 132700000 171300000 122400000 83600000 120800000        NA        NA     NA     NA
6 136300000 153700000 118100000 150200000 151700000 146900000 105500000 143700000 168400000 135200000 83600000 132000000 188900000 111600000     NA     NA

您可以输入 :

sample <- structure(list(X1.Lms = c(132500000L, 135700000L, 134100000L, 137400000L, 141300000L, 136300000L), Y1.Lms = c(132500000L, 150900000L, 136900000L, 147100000L, 144200000L, 153700000L), X2.Lms = c(114500000L, 114100000L, 113700000L, 119500000L, 115800000L, 118100000L), Y2.Lms = c(132000000L, 152000000L, 135400000L, 145600000L, 141300000L, 150200000L), X3.Lms = c(150100000L, 147500000L, 148700000L, 149500000L, 154000000L, 151700000L), Y3.Lms = c(121500000L, 142500000L, 129900000L, 143000000L, 135800000L, 146900000L ), X4.Lms = c(97600000L, 96900000L, 99600000L, 102400000L, 103900000L, 105500000L), Y4.Lms = c(123500000L, 143800000L, 127800000L, 136800000L, 132700000L, 143700000L), X5.Lms = c(164700000L, 161900000L, 164700000L, 169400000L, 171300000L, 168400000L ), Y5.Lms = c(107800000L, 128600000L, 115900000L, 128200000L, 122400000L, 135200000L), X6.Lms = c(77600000L, 78300000L, 76600000L, 84000000L, 83600000L, 83600000L), Y6.Lms = c(110400000L, 131200000L, 115100000L, 123400000L, 120800000L, 132000000L ), X7.Lms = c(181600000L, 178000000L, 183600000L, 186700000L, NA, 188900000L), Y7.Lms = c(81700000L, 100500000L, 87600000L, 106000000L, NA, 111600000L), X8.Lms = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_), Y8.Lms = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_ )), .Names = c("X1.Lms", "Y1.Lms", "X2.Lms", "Y2.Lms", "X3.Lms", "Y3.Lms", "X4.Lms", "Y4.Lms", "X5.Lms", "Y5.Lms", "X6.Lms", "Y6.Lms", "X7.Lms", "Y7.Lms", "X8.Lms", "Y8.Lms"), row.names = c(NA, 6L ), class = "data.frame")

或者我把代码 < a href=>" "http://pastebin.com/ 0ks3p2sk" rel="no follow" > on pastebin .

的“ 形状” 包中, 每一个坐标( X1. S & amp; Y1. S) 本身是一个矩阵。 也就是说, 尺寸必须是 8 x 2 x n. n 的大小是原始数据框中的行数 。

install.packages("shapes")
library(shapes)
data(gorf.dat)
gorf.dat

数据按具有尺寸的数组(8 x 2 x n)排列。

, , 1

       [,1] [,2]
 [1,]    5  193
 [2,]   53  -27
 [3,]    0    0
 [4,]    0   33
 [5,]   -2  105
 [6,]   18  176
 [7,]   72  114
 [8,]   92   38

, , 2

        [,1] [,2]
  [1,]   51  191
  [2,]   55  -31
  [3,]    0    0
  [4,]    0   33
  [5,]   25  106
  [6,]   56  171
  [7,]   98  105
  [8,]   99   15

, , 3

      [,1] [,2]
[1,]   36  187
[2,]   59  -31
[3,]    0    0
[4,]    0   36
[5,]   12  102
[6,]   38  171
[7,]   91  103
[8,]  100   19

等... 等... 等... 等... 等...

i ve tried with x <- as.array(sample, dim=...) but i m not able to find a solution so far. i guess i have to try something similar thant the transpose function in ms excel.

greetings, luke

- - - EDIT - -

你重新回答很有帮助, 但我猜你误会了我。 第一个人有8个Landmarks, 坐标16(X1, Y1, X2, Y2, X2, X3, Y3, Y3....),

, , 1
      [,1]       [,2]
 132500000  132500000
 114500000  132000000
 150100000  121500000
  97600000  123500000
 164700000  107800000
  77600000  110400000
 181600000   81700000
        NA         NA


, , 2
        [,1]       [,2]
   135700000  150900000
   114100000  152000000
   147500000  142500000
   969000000  143800000
   161900000  128600000
    78300000  131200000 
   178000000  100500000
          NA         NA

第一行(aka first case)有8个带有16个坐标的Landmarks。 之后所有标记都应储存在一个矩阵中。 之后,我想将所有案例( & gt; 100)合并成一个阵列, 每个人在那里都有自己的矩阵。

感谢迅速回应的人!

最佳回答

尝试此 :

nlandmarks = 8; nind = nrow(sample)
array(t(sample[,c(1:nlandmarks*2 -1, 1:nlandmarks * 2)]), 
  dim = c(nlandmarks, 2, nind))
问题回答

修改后处理新的数据结构:

sdat <- apply(sample, 1,matrix, ncol=2, byrow=TRUE ) #unfolds the interleaved columns
dim(sdat) <- c(8,2,6)  # expands 16 x 6 into one X column and one Y column per slice
#-----------------
> sdat[, , 1:2]
, , 1

          [,1]      [,2]
[1,] 132500000 132500000
[2,] 114500000 132000000
[3,] 150100000 121500000
[4,]  97600000 123500000
[5,] 164700000 107800000
[6,]  77600000 110400000
[7,] 181600000  81700000
[8,]        NA        NA

, , 2

          [,1]      [,2]
[1,] 135700000 150900000
[2,] 114100000 152000000
[3,] 147500000 142500000
[4,]  96900000 143800000
[5,] 161900000 128600000
[6,]  78300000 131200000
[7,] 178000000 100500000
[8,]        NA        NA


    .... remaining 4 slices omitted




相关问题
Bounding ellipse

I have been given an assignement for a graphics module, one part of which is to calculate the minimum bounding ellipse of a set of arbitrary shapes. The ellipse doesn t have to be axis aligned. This ...

Line segment in a triangle

How can we check if a line segment falls partially or fully inside a triangle? Cheers.

Line Segments from a point

I have a point p, and 2 line segments in a 2D plane. Point p is a location of view from where camera is looking towards the line segments. I want to check if line segment 1 is partially or fully ...

Creating a surface of triangles from a set of 2D points

I have a set of points and I need to convert the set to (non-overlapping) triangles (or a big polygon if equivalent)... The application: I have a list of locations (latitude,longitude) from a country,...

Radial plotting algorithm

I have to write an algorithm in AS3.0 that plots the location of points radially. I d like to input a radius and an angle at which the point should be placed. Obviously I remember from geometry ...

Delete holes in a Polygon

I have a polygon determined by an Array of Points. This polygon is crossing itself making some holes in the polygon itself. My questions is: How can I omit this holes and just get the exterior ...

Finding cycle of 3 nodes ( or triangles) in a graph

I am working with complex networks. I want to find group of nodes which forms a cycle of 3 nodes (or triangles) in a given graph. As my graph contains about million edges, using a simple iterative ...

热门标签