0

I want to change column name (from T2 [?C] to T2 [°C]) of data stored in .csv. I am using following code in R

setwd("D:/Test")
fs::dir_tree()

dirlist <- list.files(full.names = FALSE, no.. = TRUE) 
dirlist

read_the_files <- function(filelist){
  lapply(filelist, function(file) {
    fread(file, skip = "Date/Time\tXY [XY]")
  })
}

adj_col_nam <- function(dt_list){
 lapply(dt_list, FUN = function(x){
   setnames(x, old = "T2 [?C]", new = "T2 [°C]", skip_absent = TRUE)

return(x)
  })
}

In the output, it does change the column name but to T2 (°C). Why it is returning it with an additional  sign? How can I get only T2 [°C]?

Following are the updated details as per the comment:

dput(head(dt_list[[1]]))
Error in head(dt_list[[1]]) : object 'dt_list' not found

dput(head(read_the_files[[1]]))
Error in read_the_files[[1]] : object of type 'closure' is not subsettable

dput(head(filelist[[1]]))
"NYORK/NYORK_2005-01_0100_DC_mixed.txt"

dput(head(file[[1]]))
Error in file[[1]] : object of type 'closure' is not subsettable

Sys.getlocale()
[1]"LC_COLLATE=English_India.utf8;LC_CTYPE=English_India.utf8;LC_MONETARY=English_India.utf8;LC_NUMERIC=C;LC_TIME=English_India.utf8"

Sample data

Date/Time   Press [Pa]  T2 [?C]
2004-08-01S00:00:00 1013    0
2004-08-01S00:01:00 1013    0
2004-08-01S00:02:00 1013    0
2004-08-01S00:03:00 1013    0
  • 1
    Probably it's a unicode encoding issue – can you include a minimal, reproducible of your problem so we can investigate? Pasting the output of `dput(head(dt_list[[1]]))` should be fine. Also, what is the output of `Sys.getlocale()`? – divibisan Jun 21 '23 at 17:09
  • @divibisan Sorry for responding late. Please see the question. I have updated the requested details. – Alexia k Boston Jul 05 '23 at 21:01
  • It says "object 'dt_list' not found". What is your list actually called? Can you share the data from that, so we can see what the current value is? – divibisan Jul 06 '23 at 14:58
  • I have updated my code. Strangely, no list gives the desired answer. Could you suggest something helpful – Alexia k Boston Jul 06 '23 at 18:50
  • You need to show an example of your data. Somewhere you have a data frame with `T2 [?C]` in the colnames. Give us `dput(head(THAT.DATA.FRAME))`. – divibisan Jul 06 '23 at 20:01
  • You could also try entering the unicode character itself: `'\u00B0'` as in this answer: https://stackoverflow.com/a/60733863/8366499 – divibisan Jul 06 '23 at 20:03
  • I have not saved the data in dataframe but as objects. I tried for dput output of read_the_files and filelist but it does not give the required detail. I tried 'setnames(x, old = "T2 [?C]", new = "T2 [°C], '\u00B0'", skip_absent = TRUE) but it did not work. It still gives T2 (°C) in output. Did I put unicode character correctly? Just to inform you that I save my script with UTF-8(System default). Could you suggest anything to resolve it. – Alexia k Boston Jul 09 '23 at 02:23
  • Not without being able to reproduce the problem. The real solution is probably importing the table correctly. Check the file to see what the character looks like in the original CSV. Then check the encoding when you read the table in. You could use the `utf8ToInt` function to see exactly what is inside the brackts that is redered as `?` – it might not actually be a question mark. Finally you can use the unicode character instead of the degree symbol (however you put it in) ex: `"T2 [\u00B0lC]"` – divibisan Jul 10 '23 at 16:40
  • @divibisan Could you please guide me with the process how to do it. I read one file using read.table and saved it as 'File2'. Then I saved an object 'x' <- File2$T2. When I am trying utf8ToInt(x), it is giving error. And so does utf8ToInt(T2 [?C]). – Alexia k Boston Jul 10 '23 at 17:25

0 Answers0