I want to import a .csv file and switch the decimal comma to a point. Depending on the format I use to separate columns in the .csv file, can the program not read the file though, or if I change the decimal mark, I lose my format.
I have tried using gsub(",", ".", df):
data <- read.csv("D:\\My\\data\\pathway")
data_point <- as.data.frame(gsub(",", ".", data))
However, here I am encountering the issue, that the file I want to read has commas to separate the columns, as well. I have tried changing these to ;, :, tab, and tabulator (all the other options I had), when exporting the .csv file from the spread sheet. But with these the programm refused to read the data altogether:
Error in read.table(file = file, header = header, sep = sep, quote = quote, :
more columns than column names
To me this doesn't make sense, because if anything there should be more columns than column names when it has commas as decimal marks as well as separations. However, somehow this is the only format it accepts, but then I can not change the decimals to a point, only by losing all the columns. Because then everything is changed to a point and thus, I only have one big column.
Does anyone know a way to either
- import/read the file with ;, :, or tab as column separations, so the programm will accept it?
- change only the decimal commas to points but keep the column separations?
Here are the first few rows of the data (.csv) as reference:
Date & Time,Temp(C),Depth(m),Salinity(psu),Conduct(mS/cm),Sound Velocity(m/sec) "44843,96458","19,01","-0,43","0,01",0,"1478,63" "44843,96493","17,7","-0,54","0,01",0,"1474,38" "44843,96528","16,65","-0,54","0,01",0,"1470,88" "44843,96563","15,71","-0,54","0,01",0,"1467,66" "44843,96597","14,83","-0,59","0,01",0,"1464,56" "44843,96632","14,12","-0,59","0,01",0,"1462,03"
P.s. By now I have managed to get around the issue of the decimal comma, by first converting it into a matrix, and then using gsub:
#transforming the read data into matrix
mx <- as.matrix.data.frame(data)
#exchanging comma for point
mx_point <- gsub(",", ".", mx)
It is probably not very elegant, but this is how far I've gotten. The next step I tried was converting this matrix now into a data frame and working with that, however I don't know if that is even necessary?
#transforming matrix into data frame
mx_df <-as.data.frame(mx_point)
mx_df[2]<-as.numeric(bpr_mx_df[2])
Either way, as soon as I try to convert it into a numeric, it gives me another error, so I'm still missing something, or took the wrong turn along the way.
Error: 'list' object cannot be coerced to type 'double'