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
- Try add a new line between the end bracket and your
if
- 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!)