Fixing R Error in file(file, “rt”) : invalid ‘description’ argument

In r programming, the “error in filefile rt : invalid description argument” error message can occur when you try to read more than one data file using the read.csv2 function. Fixing this problem simply requires reading the files one at a time. While it is an unusual problem, it is easy to see how it can happen by mistake.

Description of the error

The “error in filefile rt : invalid description argument” error message occurs when using the read.csv2 r function along with the paste0 function in an effort to read more than one file at the same time. The read.csv2 r function normally has the file path and file name but can work with just the file name if its path puts it in the current working directory. This is because you do not need to include a path when accessing a file in the working directory. The file path and file name are usually contained in a single character string. The file path indicates the directory that the file will be found in. A CSV file is a text file that holds the contents of a data frame including each row, column, and column name.

Explanation of the error

Here is an example of some r code that produces the “error in filefile rt : invalid description argument” error message.

> df = data.frame(A = c(“A”,”B”,”C”,”D”,”E”,”F”,”G”),
+ B = c(1, 2, 3, 4, 5, 6, 7),
+ C = c(2, 3, 4, 5, 6, 7, 8),
+ D = c(2, 4, 6, 8,10,14,16))
> df
A B C D
1 A 1 2 2
2 B 2 3 4
3 C 3 4 6
4 D 4 5 8
5 E 5 6 10
6 F 6 7 14
7 G 7 8 16
> write.csv(df, “1.csv”, row.names = FALSE)
> write.csv(df, “2.csv”, row.names = FALSE)
> read.csv2(paste0(1:2, “.csv”))
Error in file(file, “rt”) : invalid ‘description’ argument

In this example, we create two CSV files from the same data frame to illustrate that this problem does not occur because of a missing source file. As you can see the paste0 function is embedded has an argument in the read.csv2 function. This is what causes the error because you cannot access more than one file this way.

How to fix the error

Here we have an r code example that fixes the “error in filefile rt : invalid description argument” error message.

> df = data.frame(A = c(“A”,”B”,”C”,”D”,”E”,”F”,”G”),
+ B = c(1, 2, 3, 4, 5, 6, 7),
+ C = c(2, 3, 4, 5, 6, 7, 8),
+ D = c(2, 4, 6, 8,10,14,16))
> df
A B C D
1 A 1 2 2
2 B 2 3 4
3 C 3 4 6
4 D 4 5 8
5 E 5 6 10
6 F 6 7 14
7 G 7 8 16
> write.csv(df, “1.csv”, row.names = FALSE)
> write.csv(df, “2.csv”, row.names = FALSE)
> a = paste0(1:2, “.csv”)
> for (n in c(1,2)) {
+ print(read.csv2(a[n]))
+ cat(sep=”\n\n”)
+ }
A.B.C.D
1 A,1,2,2
2 B,2,3,4
3 C,3,4,6
4 D,4,5,8
5 E,5,6,10
6 F,6,7,14
7 G,7,8,16

A.B.C.D
1 A,1,2,2
2 B,2,3,4
3 C,3,4,6
4 D,4,5,8
5 E,5,6,10
6 F,6,7,14
7 G,7,8,16

Like before we create two CSV files from the same data frame So that they will be available to be read later. However instead of embedding the two functions, this time they are used separately but with a for loop so that the read.csv2 function can read each CSV file separately. It still uses the paste0 function, but it is used to create a vector from which each filename is read separately. This works without any problems, while still allowing for easy adjustment to reading more files.

The “error in filefile rt : invalid description argument” error message is one that you are not likely to get because it requires specific circumstances to occur. However, once you understand this problem it is easy to fix should it occur. Furthermore, you are more likely to avoid it once you are aware of it and know how to fix it.

Scroll to top
Privacy Policy