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!

5
  • 2
    Don't do this. Collect all your similar results into a named list. Something more like this: stackoverflow.com/questions/17499013/…. It will be so much easier for you to work with in the long run. Commented Mar 12, 2018 at 18:55
  • Also, when asking for help, you should include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions. Make sure all variables are defined. Commented Mar 12, 2018 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. Commented Mar 12, 2018 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? Commented Mar 12, 2018 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? Commented Mar 12, 2018 at 19:12

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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 = '')))

1 Comment

Hi there, thanks for your answer! Unfortunately, I tried this code and it did not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.