0

I have hardcoded this:

s79t5 <- read.csv("filename1.csv", header = TRUE)
s81t2 <- read.csv("filename2.csv", header = TRUE)
etc.

subsets79t5 <- subset(s79t5, Tags!='')
subsets81t2 <- subset(s81t2, Tags!='')
...
subsets100t5 <- subset(s100t5, Tags!='')

now i need to softcode it. i am almost there:

sessions <- c('s79t5', 's81t2', 's88t2', 's90t3', 's96t3', 's98t4', 's100t5')

for (i in 1:length(sessions)) {
    jFileName <- c(as.character(sessions[i]))
    j <- data.frame(jFileName)
    subset <- subset(j, j$Tags!='')
    assign(paste("subset", jFileName, sep = ""), data.frame(subset))
}
Frank
  • 66,179
  • 8
  • 96
  • 180
wade12
  • 3
  • 4
  • Hi there. Could you better explain what exactly you are trying to do? I'm not quite following what your question is. Also providing a sample of your data (possibly with the `head` function) would be helpful as well. – giraffehere Apr 15 '16 at 15:16
  • i have created these data frames: subsets79t5, subsets81t2, etc. but they are hard-coded. Now, i need to soft code them, i.e. create them using a for loop. thanks. – wade12 Apr 15 '16 at 15:19
  • Gregor's very long answer here might be a good place to start: http://stackoverflow.com/a/24376207/1191259 – Frank Apr 15 '16 at 15:20
  • @wade12 It looks like your code creates the data frames, though they are empty and of only 1 variable. What is your output supposed to look like? Are you getting an error of some sort? – giraffehere Apr 15 '16 at 15:21
  • s79t5 is 3000 observations of 12 variables, so subsets79t5 is a subset of this containing only rows with values in the tag column. no error. just need to fix this line of code: j <- data.frame(jFileName) so that j = s79t5 from above, but obviously changes on each iteration through the loop. – wade12 Apr 15 '16 at 15:25
  • Okay, so where are you going wrong? Also, as a note, in `assign` I don't believe you have to do `data.frame(subset)`, you can just do `subset`. Though it may be better to not name your objects the same as a function you are using (just for clarity). – giraffehere Apr 15 '16 at 15:27
  • Also, perhaps what you are missing is the `get` function. Are you just generating empty data frames that aren't being referenced to your data? If so, use `get` in your loop as so: `j <- as.data.frame(get(jFileName))`. This will grab your object that is the same name as jFileName and coerce it to a data frame. – giraffehere Apr 15 '16 at 15:29
  • good point about clarity, will change the name, thanks. – wade12 Apr 15 '16 at 15:30
  • See my comment above this, I think it's what you need. You also don't need to wrap `as.character(session[i])` in `c()`. Just doing `jFileName <- as.character(session[i])` is fine. – giraffehere Apr 15 '16 at 15:30
  • 1
    perfect. i was missing "get". many thanks giraffehere, you are an absolute star, have a super weekend. – wade12 Apr 15 '16 at 15:32
  • wow! talk about fast ... i love stackoverflow even more now. cheers. – wade12 Apr 15 '16 at 15:32
  • It's a great place. Just make sure you clearly define what your input is like and what you'd like as output, what your question is, and what you think might be missing. I kind of had to tease the issue out of you, haha. ;) Have a great weekend. – giraffehere Apr 15 '16 at 15:33
  • sure, will do, many thanks. – wade12 Apr 15 '16 at 15:34
  • @giraffehere If you think the question is unclear, you should encourage the OP to edit the question to clarify it, not just to elaborate in comments. (Personally, I think it was understandable as initially posted.) – Frank Apr 15 '16 at 16:55

1 Answers1

0

Just throwing an answer here to close this question. Discussion was in the comments.

You need the get function in your line: j <- data.frame(jFileName)

It should be: j <- as.data.frame(get(jFileName))

The get function looks in your existing objects for the string character you gave it (in this case, jFileName) and returns that object. I then make sure it is a data frame with as.data.frame.

Previously you were essentially telling R to make a data frame out of a character string. With get you are now referencing your actual dataset.

giraffehere
  • 1,118
  • 7
  • 18