0

Im a new programmer for R and im having problems integrating data. Im trying to filter data from 3 subjects, each file starts with the subject name ( ie Subj2xxx) and im trying to sort them by using an extract of the name with a loop than write the file to a data frame that is named according to what is in it here is my code, can anyone tell me what im doing wrong?

setwd("J:\\Data Capture\\Assignment\\Temp Assignment data\\")
data_list <- ("J:\\Data Capture\\Assignment\\Temp Assignment data\\",recursive=TRUE)

for(i in length(data_list)){
if(grep(subj1,data_list,fixed=TRUE)){
data.frame(read.table(print(i))) -> paste(i)
} if(grep(subj2,data_list,Fixed=TRUE)){
data.frame(read.table(print(i))) -> paste(i)
} if(grep(subj2,data_list,Fixed=TRUE)){
data.frame(read.table(print(i))) -> paste(i)
}
}

When I run this through R, I get these error ( I've attached a screenshot as there are many of them) errors please let me know if this is the right way to upload these type of questions

Thanks in advance

Oliver
  • 8,169
  • 3
  • 15
  • 37
Damo H
  • 77
  • 6
  • 1
    Welcome to SO. No, we can't be certain what you're doing wrong because we can't read files from your J drive and you haven't told us what happens (errors? incorrect output?) when you run your code. I do notice, though, that you haven't closed your `for` loop. You maximise your chance of getting a useful answer if you provide a minimum reproducible example. [This post](https://stackoverflow.com/help/minimal-reproducible-example) may help. – Limey Mar 17 '21 at 08:09
  • 1
    Welcome to SO. Please edit your question and fix all of the following: 1) Your loop isn't closed, 2) your path in `setwd` needs quotes, 3) your comments are not commented with hashtags, 4) `assign(i) -> file_location` doesn't make sense and throws an error. 5) you have `n, y, z` but only use n. It seems some code is missing. 6) Add the error message to your question, in order for us to help. We can only (maybe) help you ones 1 through 6 has been fixed. – Oliver Mar 17 '21 at 08:11

1 Answers1

0

Lets take a look at your code and go through the problem line by line. The first couple of things are difficult to see directly, but common errors when starting out.

setwd("J:\\Data Capture\\Assignment\\Temp Assignment data\\")

For this part, the error is "likely" to be a wrong quotation mark. " seems correct but maybe you are using 2 single quotes '. In R the quotes are interchangeable, and starting one double quote " or a single quote ' indicates the start of a string. But notice that it is 2 different symbols, and a double quote is not the same as 2 single quotes. (Find the button on your keyboard for double quotes. On windows EU it is shift+2). There is also a very similar quote with slighlty "flowy" looks, which is not taken as a "real" quotation in the R sense on all platforms

data_list <- ("J:\\Data Capture\\Assignment\\Temp Assignment data\\",recursive=TRUE)

You are likely looking for list.files here, not just the parenthesis. Because you used setwd you can exclude the path (it will start the search in your current working directory). Again the error is potentially talking about the double single quotes being wrong.

data_list <- list.files(recursive = TRUE) 
for(i in length(data_list)){
if(grep(subj1,data_list,fixed=TRUE)){
data.frame(read.table(print(i))) -> paste(i)
} if(grep(subj2,data_list,Fixed=TRUE)){

The error states very clearly that "Unexpected if in } if(". So try adding a new line. You can

  1. Try add a new line between the end bracket and your if
  2. Or add an else in front of your if.

The latter is likely what you're looking for.

if(x){
# do something
}else if(y){
# do something else
} else {
# do something third if not x and not y
}`

is the general syntax for if-else statements. There are plenty guides out there (w3school has one that is well written). Try looking up guides on topics online whenever you have to do something new in R, and alter the examples to your specific problem. It is where we all start out. :-)

Most other errors

The remaining errors come from not writing your code properly. Code is case-sensitive so Data.frame is not the same as data.frame. In you error image it is very clear that you have both misspelled I = i and Data.frame = data.frame. There may be other places I haven't spotted.

Errors not shown

There is one error that is hidden in your code (not shown in the console). Assignment goes into a target variable not into paste or similar,

data.frame(read.table(print(i))) -> paste(i)

is thus not valid code.

'file' must be a character string or connection

In your loop you are not iterating over files (you are actually not iterating at all). The general syntax of a for-loop is

for(i in thing-to-loop-over){
 #do something
}

you loop however looks like for(i in length(data_list)). length(data_list) is a number, so i will take only a single value. In R you don't even need to iterate over a numeric value, you could just do for(i in data_list), and then i will iteratively take the values in data_list (in order).

Now your problem is actually more severe, as you are trying to read multiple data.frames. You can't do your assignment iteratively like you are. What you should do is save them to a list and extract the individual frames from this list. This answer shows exactly how to do so (alter to your problem!)

Oliver
  • 8,169
  • 3
  • 15
  • 37
  • Thanks Oliver! Youve helped alot! :) I think i understand it alot more after reading that – Damo H Mar 18 '21 at 09:40
  • My pleasure. Remember that in these early stages each wall broken down is a major leap in understanding how to crush the next one. ;-) – Oliver Mar 18 '21 at 09:44
  • A quick side note @DamoH. I found it very helpful to try and print my output when I started getting confused (both in for-loops and functions etc.). Something like `for(i in something-to-iterate-over) print(i)` and then comparing it whatever you're iterating over, can help understanding how it works. Later there are better methods but early on I found this more intuitive. – Oliver Mar 18 '21 at 09:49