Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8014351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:54:25+00:00 2026-06-04T19:54:25+00:00

i’d like to know how to transform a data.frame into an array in R.

  • 0

i’d like to know how to transform a data.frame into an array in R. therefor my data.frame consists of 16 columns and >100 rows. every row stands for one individual (snail). i did put 8 landmarks on each snail. so now i’ve got 16 coordinates/individual. no i want to transform the data.fram into an array, where each individual gets its own matrix and all individuals are put together in one (big) array.

my data looks like this: 6 individuals with 8 landmarks aka 16 coordinates (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

you can get this table into R by entering:

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")

alternatively i’ve put the code on pastebin.

the result should look like the sample data of gorilla females in the “shapes” package, where every coordinate (X1.S & Y1.S) is a matrix on its own. i guess the dimensions have to be (8 x 2 x n). n is the number of rows in the original data.frame.

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

the data is organized as an array with the dimensions (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

and so on….

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 —

you’re answers helped a lot, but i guess you’ve got me wrong. the first individual has got 8 Landmarks with 16 coordinates (X1, Y1, X2, Y2, X3, Y3….). so to be absolutely clear on my desired results :)… what i have in mind should look more like this:

, , 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

the first row (aka first case) has 8 Landmarks with 16 coordinates. all of them should be stored in a matrix. after that i want to combine all cases (>100) into an array where each individual has its own matrix.

thanks for the quick response guys!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:54:26+00:00Added an answer on June 4, 2026 at 7:54 pm

    Try this:

    nlandmarks = 8; nind = nrow(sample)
    array(t(sample[,c(1:nlandmarks*2 -1, 1:nlandmarks * 2)]), 
      dim = c(nlandmarks, 2, nind))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.