1

I have a dataset/table (called behavioural) with data from 24 participants - these are named in the format: 's1' to 's24'.

The first 6 rows in the table/dataset:

head(behavioural)[c(1,17)]
   subj     recognition_order
1   s1                 2
2   s1                 6
3   s1                 7
4   s1                 8
5   s1                 9
6   s1                10

I want to create a subset for each participant and order each of these subsets by the variable recognition_order

I have created a loop to do this:

behavioural <- read.table("*my file path*\behavioral.txt", header = TRUE)

subj_counter <- 1


for(i in 1:24) {
subject <- paste("s", subj_counter, sep = "")
subset_name <- paste(subject, "_subset", sep="")

[subset_name] <- behavioural[which(behavioural$subj  == subject), ]
[subset_name] <- subset_name[order(subset_name$recognition_order),]

subj_counter = subj_counter + 1

print(subset_name)
print(subj_counter)
}

And I'm pretty sure the logic is solid, except when I run the loop, it does not create 24 subsets. It just creates 1 - s24_subset.

What do I need to do to the bit before "<-" in these 2 lines of code?

[subset_name] <- behavioural[which(behavioural$subj  == subject), ]
[subset_name] <- subset_name[order(subset_name$recognition_order),]

Because [subset_name] isn't working.

I want the [subset_name] to be dynamic - i.e. each time the loop runs, its value changes and it creates a new subset/variable each time.

I have seen things online about the assign() function but I'm not quite sure how to implement this into my loop?

Thank you in advance!

EdinPC
  • 23
  • 2
  • 3
  • 9
  • 2
    Don't do this. Collect all your similar results into a named list. Something more like this: https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames. It will be so much easier for you to work with in the long run. – MrFlick Mar 12 '18 at 18:55
  • Also, when asking for help, you should 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. Make sure all variables are defined. – MrFlick Mar 12 '18 at 18:57
  • 1
    I would suggest using the `split` function to split your original dataset into a list with the 24 subset. It is much easier to would with a list (using lapply) then the 24 separate dataframes. – Dave2e Mar 12 '18 at 18:58
  • Mr Flick - the entire code I posted above could be run straight away on someone else's computer, provided they have the 'behavioural.txt' file, but how do I put/attach the text file to this question? – EdinPC Mar 12 '18 at 18:59
  • @Dave2e - do you mean something like this? `my_split_list <- split(behavioural, behavioural$subj)`'This creates a very large list, but how do I then sort this by 'recognition_order' per subject? – EdinPC Mar 12 '18 at 19:12

3 Answers3

4

If you want to order the items inside the results of a split than just use lapply to pass the needed function calls to do the ordering on a single dataframe at a time (which are re-bundled together by lapply after the ordering:

my_split_list <- split(behavioural, behavioural$subj)
ord.list <- lapply( my_split_list, function(d){ 
                   d[ order(d[['recognition_order']]) , ] }

This is a common paradigm called "split-apply-combine": "The Split-Apply-Combine Strategy for Data Analysis" https://www.jstatsoft.org/article/view/v040i01/v40i01.pdf

IRTFM
  • 258,963
  • 21
  • 364
  • 487
3
for(i in 1:5) {
  assign(paste0("test_",i),i) 
}
test_list <- mget(ls(pattern = "test_"))`

I hope you get a good answer. There is a good way to create an assign function and to bind this pattern in mget. I've outlined a lot of questions for R and Python about dynamically generating variables. Attached to the following link. - Creating Variables Dynamically (R, Python)

THess
  • 1,003
  • 1
  • 13
  • 21
shjj08
  • 31
  • 1
0

You can accomplish this with eval() and parse(), like so:

eval(parse(text = paste(subset_name, "<- subset_name[order(subset_name$recognition_order),]", sep = '')))
93i7hdjb
  • 1,136
  • 1
  • 9
  • 15