0

With the code below, I have imported all .txt files from working directory.

temp=list.files(pattern = "*.txt")
for (i in 1:length(temp)) { assign(temp[i], read.delim(temp[i]))

But all of them came with .txt extension like this. screenshot

How can I remove all .txt extensions from data names?

NelsonGon
  • 13,015
  • 7
  • 27
  • 57

1 Answers1

2

You can rename the variables in your for loop itself

for (i in 1:length(temp)) {assign(sub(".txt$", "", temp[i]), read.delim(temp[i]))}

Or if you have already imported the variables change their names later

vals <- ls(pattern = ".txt$")
for (i in vals) { assign(sub(".txt$", "", i), get(i)) }

and then clean up the old names

rm(list = vals)

On a side note, using assign is considered bad. Read it's potential dangers and side effects here.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213