0

I have a table4 with 25 columns and I do want to subset this table for 7 values in column Job..R to get 7 individual tables.

for(i in 1:7) {
  x = NULL
  x = list(1 == "14047/2", 2 == "18331/3", 3 == "18331/4", 4 == "24889/3", 
           5 == "24889/4", 6 == "24889/5", 7 == "24889/9")
  View(i)
  a = subset(table4, Job..R == x[i])
  b = mean(a[ ,13])
  View(a)
  View(b)
  save(b, file="i.Rda") #ideally save .Rda file with used Job..R
}

I created a for loop from 1 to 7 and a list with names 1 to 7 and to each name I have assigned a value from column Job..R I want to subset to.

At the moment this loop does 7 loops (that is what I want) and creates me 7 tables with the 25 columns including headers but without content. How can I get the content into the new tables? And how can I save the new tables with a specific file name for each table. Ideally the specific file name is the number from column Job..R I used to subset with. Many thanks in advance!!

Johannes

1
  • 3
    hello, and welcome to stackoverflow. Please consider rewording your question as it lacks clarity. Commented Aug 4, 2014 at 3:18

1 Answer 1

1

Don't use == inside your list() call as it compares values rather than assigning list items. x using your current code is just a list of FALSE values because:

1=="14047/2"
#[1] FALSE

Try this instead:

x = list("14047/2","18331/3","18331/4","24889/3", "24889/4","24889/5","24889/9")

Also, you will need to paste0 together your file name so that the i can be interpreted properly:

save(b, file=paste0(i,".Rda") )
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, the sub setting works now, but when I do the save command then the console comes up with the following: 115: save(a, paste0(i,".Rda") #ideally save .Rda file with used Job..R 116: } ^ It complaints about the closing bracket, @thelatemail
@Johannes - did you add a second closing bracket just like the example code I posted? It doesn't look like your code has one.
I found this as well and tried to delete my comment, but now the following error message appears: Error in save(a, paste0(i, ".Rda")) : object ‘paste0(i, ".Rda")’ not found.
@thelatemail that last line should be save(b, file=paste0(i,".Rda") )

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.