0

I am having a bit of trouble converting a data frame to an array.

I am doing EEG analysis, and I want to use an R package that uses array as its default data table for some computations. However, what I have is a data frame. Here's the example of my data frame. Figure 1

I would like to ask how can I format this into an array, with the headers of the data frame as the start, similar to this, plus the number/ range of items beside the class (e.g. char or factor). Figure 2

I tried using the array() function but it did not work, the class was still a "data frame", the headings were gone and no number how many there are.

I would appreciate any help regarding this matter. Thank you!

SRE:

So, I have all the dataset as about a participants' ID, electrodes (channel), hemisphere (part of the brain) and some stimuli components (segment, class, type),time and amplitude of the effect I needed. Basically the data frame looks like this:

> data_eeg_long
# A tibble: 2,496,000 x 9
   ID    timing channel hemisphere segment stimclass pairtype  time    ampl
   <fct> <chr>  <chr>   <chr>      <chr>   <chr>     <chr>    <dbl>   <dbl>
 1 01    long   Fp1     left       Ref     Arm       Df        -100 -0.776 
 2 01    long   Fz      center     Ref     Arm       Df        -100 -0.219 
 3 01    long   F3      left       Ref     Arm       Df        -100 -0.356 

Now, I want to use this package command to get the GFP for these values. However, it needs to be an array (such as what I have shown in Figure 2) or a numerical matrix. However, when I try to convert these to an array using the following:

> eeg_long_array <- array(data_eeg_long, dimnames= eeg_long_matrix[1,])
> head(eeg_long_array)                                   
1 01 long Fp1   left Ref Arm Df -100 -0.7756
2 01 long  Fz center Ref Arm Df -100 -0.2190
3 01 long  F3   left Ref Arm Df -100 -0.3563
4 01 long  F7   left Ref Arm Df -100 -0.6495
5 01 long FT9   left Ref Arm Df -100 -0.4021
6 01 long FC5   left Ref Arm Df -100 -0.3363
> class(eeg_long_array)
[1] "data.frame"

And it looks like this in my environment: enter image description here

It still does not work. The output I expect is similar to the Figure 2 I showed, but I just cannot figure out how. I am new to this package, but it's the only one available that can make the computation I need.

Mish Lee
  • 21
  • 1
  • 1
  • 3
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. If you have a data.frame, all the columns will have the same length. And arrays don't let you mix numeric and character values. They must all be the same type. Are you sure you want an array? – MrFlick Jun 25 '21 at 17:12
  • Hello, @MrFlick, I added an SRE, thank you. And yes, it should be an array. With regards to the mixture of numeric and character values, that I am not sure of, because the other examples I have were able to do it in a mix. I am new to the "array" concept as I mostly dealt with data frames. Although as for the length, yes, my values differ per type (e.g. participants 6, but amplitude has 700 points/px) – Mish Lee Jun 25 '21 at 17:59
  • Have you looked at the `xtabs()` function? – guyabel Jul 09 '21 at 02:22

1 Answers1

-1

In python3, a naive way would be to traverse down the data frame and then include those elements in 2d list. Then apply array on the list. I think much better ways exists and waiting for others to suggest some.