2

I have the array:

>cent
       b    e          r    f
A19 60.46   0.77    -0.12   1
A15 16.50   0.53    0.08    2
A17 2.66    0.51    0.20    3
A11 36.66   0.40    -0.25   4
A12 38.96   0.91    0.23    1
A05 0.00    0.29    0.01    2
A09 3.40    0.35    0.03    3
A04 0.00    0.25    -0.03   4

Could some one please say me how to split this array into 4 separate arrays where the last column «f» is the flag? In result I would like to see:

>cent1
       b    e          r    f
A19 60.46   0.77    -0.12   1
A12 38.96   0.91    0.23    1
>cent2
       b    e          r    f
A15 16.50   0.53    0.08    2
A05 0.00    0.29    0.01    2
….

Should I use the for-loop and check flag "f" or exist a build-in function? Thanks.

1
  • If it's really an array, and not a data frame, then try this one Commented Jan 16, 2016 at 17:58

1 Answer 1

1

We can use split to create a list of data.frames.

 lst <- split(cent, cent$f)

NOTE: Here I assumed that the 'cent' is a data.frame. If it is a matrix

 lst <- split(as.data.frame(cent), cent[,"f"])

Usually, it is enough to do most of the analysis. But, if we need to create multiple objects in the global environment, we can use list2env (not recommended)

 list2env(lst, paste0("cent", seq_along(lst)), envir= .GlobalEnv)
Sign up to request clarification or add additional context in comments.

3 Comments

thanks you for reply. The function split() is work. Now I have the list lst. How can I address to elements in the list lst[1] that are of the same type, say $b?
I have tried lst[1]$b, lst['1'], lst[1]$'1'. But I need to extract first column only.
@Nick If you need the first column lst[[1]][1] (if the dataset is data.frame) , if is is matrix lst[[1]][,1]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.