converting multiple columns from character to numeric format in r
You could try
DF <- data.frame("a" = as.character(0:5),
"b" = paste(0:5, ".1", sep = ""),
"c" = letters[1:6],
stringsAsFactors = FALSE)
# Check columns classes
sapply(DF, class)
# a b c
# "character" "character" "character"
cols.num <- c("a","b")
DF[cols.num] <- sapply(DF[cols.num],as.numeric)
sapply(DF, class)
# a b c
# "numeric" "numeric" "character"
Convert multiple columns of a data frame from string to numeric in R
We can use lapply
to loop through the columns and apply as.numeric
df[cols] <- lapply(df[cols], as.numeric)
where
cols <- names(df)[4:10] # or column index (change the index if needed)
Permanently convert multiple columns to numeric - dplyr
Use this instead -- across
did not exist 7 years ago when the link in the question was written:
df %>% mutate(across(.fns = type.convert))
or with only base R:
replace(df, TRUE, lapply(df, type.convert))
Coercing all but two columns from character into numeric
Try this base R
solution:
df[,3:43] <- apply(df[,3:43],2,as.numeric)
Convert multiple character columns with numbers and , symbols into numeric in R
You can apply this approach to whichever columns you want. In this case, if you want to apply to columns 1 through 3, you can specify as labor_df[1:3]
. If you want to apply to specific columns based on the column name, then create a cols
vector containing the names of columns to apply this to and use labor_df[cols]
instead.
The first gsub
will remove the greater than sign, and keep the value unchanged. The ifelse
is vectorized and will apply to all values in the column. It will first check with grepl
if less than sign is present; if it is, remove it, convert to a numeric value, and then divide by 2. Otherwise leave as is.
labor_df[1:3] <- lapply(labor_df[1:3], function(x) {
x <- gsub(">", "", x)
x <- ifelse(grepl("<", x), as.numeric(gsub("<", "", x)) / 2, x)
as.numeric(x)
})
labor_df
Output
a b c
1 1 9 20.00
2 5 14 1.99
3 12 5 14.50
Data
labor_df <- structure(list(a = c("1", "<10", "12"), b = c("9", "14", ">5"
), c = c("20", "1.99", "14.5")), class = "data.frame", row.names = c(NA,
-3L))
How do I convert all numeric columns to character type in my dataframe?
In base R
, we may either use one of the following i.e. loop over all the columns, create an if/else
conditon to change it
dataframe[] <- lapply(dataframe, function(x) if(is.numeric(x))
as.character(x) else x)
Or create an index for numeric columns and loop only on those columns and assign
i1 <- sapply(dataframe, is.numeric)
dataframe[i1] <- lapply(dataframe[i1], as.character)
It may be more flexible in dplyr
library(dplyr)
dataframe <- dataframe %>%
mutate(across(where(is.numeric), as.character))
Convert multiple character columns into numeric in R
Probably, you were trying to do :
df[cols] <- lapply(df[cols], function(x) as.integer(factor(x)))
You can also use :
df[cols] <- lapply(df[cols], function(x) match(x, unique(x)))
Converting multiple columns to double type in R using dplyr
It would be better to do this with type.convert
from base R
which automatically correct the type based on the value in each column
df1 <- type.convert(df, as.is = TRUE)
In dplyr
, it can be done with across
and specify the range of columns with either numeric index
df %>%
mutate(across(2:4, as.numeric))
Or the column names range
df %>%
mutate(across(X11:P3, as.numeric))
Related Topics
Using Data.Table Package Inside My Own Package
How to Export Multiple Data.Frame to Multiple Excel Worksheets
Workflow For Statistical Analysis and Report Writing
How to Count Runs in a Sequence
Editing Legend (Text) Labels in Ggplot
Create Discrete Color Bar With Varying Interval Widths and No Spacing Between Legend Levels
Efficiently Convert Backslash to Forward Slash in R
How to Uninstall R and Rstudio With All Packages, Settings and Everything Else
Read All Worksheets in an Excel Workbook into an R List With Data.Frames
Tools For Making Latex Tables in R
Proper/Fastest Way to Reshape a Data.Table
Error: '\R' Is an Unrecognized Escape in Character String Starting "C:\R"