Convert character columns to numeric

I have a excel file with 8 columns, and all except the first column are being read as characters. However, All columns are supposed to be numeric. I ran the code (pasted below) and to convert the character columns to numeric types. While making some changes, it somehow ran correctly once, but now it is not working. I need to change all the character columns to numeric type, and the values are all decimal numbers or integers, without any special character in the values

Code:
library(readxl)
df <- read_excel('filepath')
str(df)
for (i in c(2,3,4,5,6,7,8)){
df[,i] = as.numeric(df[,i])
}
str(df)
sapply(df, class)

I don't see anything wrong with your code. Please post the output of

dput(head(df, 25))

using the version of df immediately afterdf <- read_excel('filepath'). Put a line with three back ticks immediately before and after the output, like this:
```
output of dput()
```

I tried it on a different dataframe which has a similar structure as the previous one. This one has 5 columns, as seen in the screenshot below. I have also pasted the code below, with the dput function added to it. after that, there is a screenshot of the output given on running this code. There is no output on running the code. The console just moves to the next line.

library(xlsx)
library(readxl)
mydf <- read_excel('filepath')
str(mydf)
for (i in c(2,3,4,5)){
mydf[,i] = as.numeric(mydf[,i])
}
print(str(mydf))

dput(head(mydf,15))

Following are the first few rows of my excel file:

	Temperature	Pressure	Concentration
	oC	atm	mol/L
0	25	1	0.1
10	25	1	0.1
20	25	1	0.1
30	25	1	0.1
40	25	1	0.1
50	25	1	0.1
60	25	1	0.1
70	25	1	0.1
80	25	1	0.1
90	25	1	0.1
100	26	1.04	0.5
110	25.5	1.07	0.7
120	26	1.1	0.8
130	27	1.2	0.9

On running the code it gives the error "Error in as.numeric(mydf[, i]) :
'list' object cannot be coerced to type 'double'"

Please do not post screenshots, they are considered a bad practice here, post formated code instead. Here is how to do it:

I have edited my post and removed the screenshots. Sorry for the inconvenience, I hadn't realised about the screenshots being a bad practice earlier.

The output of dput, is your data as code. It is ideal way to share data based on the fact that the datatypes are preserved.

Data types are at the heart of your issue .

1.07 could be number or character in your data, and we woudlnt know one way or the other without seeing the result of your dput()

Hi @Saurish_Seksaria remeber put a reproducible example of your data. If I understand well you need something like that:

install.packages('dplyr')
library(dplyr)
data1 <- data.frame(
  Temperature = as.character(c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130)),
  Pressure = c(25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 25.5, 26, 27),
  Concentration = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.5, 0.7, 0.8, 0.9)
)

# For put all column like numeric type
data1 <- data1 %>%
  mutate(across(everything(), as.numeric))

str(data1)
# data.frame':	14 obs. of  3 variables:
#  $ Temperature  : num  "0" "10" "20" "30" ...
#  $ Pressure     : num  25 25 25 25 25 25 25 25 25 25 ...
#  $ Concentration: num  0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.

------------
# For select some columns and put like numeric type

data2 <- data.frame(
  Temperature = as.character(c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130)),
  Pressure = c(25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 25.5, 26, 27),
  Concentration = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.5, 0.7, 0.8, 0.9)
)

data2 <- data2 %>%
  mutate(across(c(Concentration, Pressure), as.numeric))

str(data2)
# data.frame':	14 obs. of  3 variables:
#  $ Temperature  : chr  "0" "10" "20" "30" ...
#  $ Pressure     : num  25 25 25 25 25 25 25 25 25 25 ...
#  $ Concentration: num  0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1

Thank you for the help, this code will be quite useful. Incidentally, after I restarted my laptop and ran my code again, it worked fine. Would you have any idea why this might have happened?
Also, I will remember to include a working sample of my data the next time.

I understand that it is ideal to share the data as a code that can be run easily, but the dput function was not running in my code despite several attempts, and restarting Rstudio, so I had to share it as a formatted string rather than a dput output. I am sorry for the inconvenience.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.