Check if all values are numeric over multiple columns and convert them to numeric
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a dataframe with all the columns being character like this.
ID <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
ToolID <- c("CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B",
"CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B")
Step <- c("Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F",
"Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F")
Measurement <- c("Length","Breadth","Width","Height",NA,NA,NA,NA,
"Length","Breadth","Width","Height",NA,NA,NA,NA)
Passfail <- c("Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass",
"Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass")
Points <- as.character(c(7,5,3,4,0,0,0,0,17,15,13,14,0,0,0,0))
Average <- as.character(c(7.5,6.5,7.1,6.6,NA,NA,NA,NA,17.5,16.5,17.1,16.6,NA,NA,NA,NA))
Sigma <- as.character(c(2.5,2.5,2.1,2.6,NA,NA,NA,NA,12.5,12.5,12.1,12.6,NA,NA,NA,NA))
Tool <- c("ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2",
"ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2")
Dose <- as.character(c(NA,NA,NA,NA,17.1,NA,NA,17.3,NA,NA,NA,NA,117.1,NA,NA,117.3))
Machine <- c("CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2",
"CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2")
df <- data.frame(ID,ToolID,Step,Measurement,Passfail,Points,Average,Sigma,Tool,Dose,Machine)
I am trying to check these character vectors for numeric values and then convert those with numeric values to numeric. I use the "varhandle" package in R to do it
library(varhandle)
if(all(check.numeric(df$Machine, na.rm=TRUE))){
# convert the vector to numeric
df$Machine <- as.numeric(df$Machine)
}
This works but is inefficient because I have to manually enter the column names like above. How can I do it more efficiently in a loop or use vectorization over multiple columns? My actual dataset has around 350 columns. Can someone point me in the right direction?
r dataframe dplyr data.table tidyr
|
show 2 more comments
I have a dataframe with all the columns being character like this.
ID <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
ToolID <- c("CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B",
"CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B")
Step <- c("Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F",
"Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F")
Measurement <- c("Length","Breadth","Width","Height",NA,NA,NA,NA,
"Length","Breadth","Width","Height",NA,NA,NA,NA)
Passfail <- c("Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass",
"Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass")
Points <- as.character(c(7,5,3,4,0,0,0,0,17,15,13,14,0,0,0,0))
Average <- as.character(c(7.5,6.5,7.1,6.6,NA,NA,NA,NA,17.5,16.5,17.1,16.6,NA,NA,NA,NA))
Sigma <- as.character(c(2.5,2.5,2.1,2.6,NA,NA,NA,NA,12.5,12.5,12.1,12.6,NA,NA,NA,NA))
Tool <- c("ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2",
"ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2")
Dose <- as.character(c(NA,NA,NA,NA,17.1,NA,NA,17.3,NA,NA,NA,NA,117.1,NA,NA,117.3))
Machine <- c("CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2",
"CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2")
df <- data.frame(ID,ToolID,Step,Measurement,Passfail,Points,Average,Sigma,Tool,Dose,Machine)
I am trying to check these character vectors for numeric values and then convert those with numeric values to numeric. I use the "varhandle" package in R to do it
library(varhandle)
if(all(check.numeric(df$Machine, na.rm=TRUE))){
# convert the vector to numeric
df$Machine <- as.numeric(df$Machine)
}
This works but is inefficient because I have to manually enter the column names like above. How can I do it more efficiently in a loop or use vectorization over multiple columns? My actual dataset has around 350 columns. Can someone point me in the right direction?
r dataframe dplyr data.table tidyr
1
change <- sapply(dat, function(x) all(check.numeric(x, na.rm=TRUE))
and thendat[change] <- lapply(dat[change], as.numeric)
.
– lmo
Oct 1 '18 at 13:58
@Imo Works perfectly. Thank you
– Sharath
Oct 1 '18 at 14:01
Just usetype.convert
:type.convert(df)
.
– nicola
Oct 1 '18 at 14:09
1
@nicolatype.convert(df) ; # Error in type.convert(df) : the first argument must be of mode character
Am I missing something ?
– Ronak Shah
Oct 1 '18 at 14:10
1
Yes, thedata.frame
andlist
methods oftype.convert
have been introduced with R-3.5.0
– nicola
Oct 1 '18 at 14:16
|
show 2 more comments
I have a dataframe with all the columns being character like this.
ID <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
ToolID <- c("CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B",
"CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B")
Step <- c("Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F",
"Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F")
Measurement <- c("Length","Breadth","Width","Height",NA,NA,NA,NA,
"Length","Breadth","Width","Height",NA,NA,NA,NA)
Passfail <- c("Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass",
"Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass")
Points <- as.character(c(7,5,3,4,0,0,0,0,17,15,13,14,0,0,0,0))
Average <- as.character(c(7.5,6.5,7.1,6.6,NA,NA,NA,NA,17.5,16.5,17.1,16.6,NA,NA,NA,NA))
Sigma <- as.character(c(2.5,2.5,2.1,2.6,NA,NA,NA,NA,12.5,12.5,12.1,12.6,NA,NA,NA,NA))
Tool <- c("ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2",
"ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2")
Dose <- as.character(c(NA,NA,NA,NA,17.1,NA,NA,17.3,NA,NA,NA,NA,117.1,NA,NA,117.3))
Machine <- c("CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2",
"CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2")
df <- data.frame(ID,ToolID,Step,Measurement,Passfail,Points,Average,Sigma,Tool,Dose,Machine)
I am trying to check these character vectors for numeric values and then convert those with numeric values to numeric. I use the "varhandle" package in R to do it
library(varhandle)
if(all(check.numeric(df$Machine, na.rm=TRUE))){
# convert the vector to numeric
df$Machine <- as.numeric(df$Machine)
}
This works but is inefficient because I have to manually enter the column names like above. How can I do it more efficiently in a loop or use vectorization over multiple columns? My actual dataset has around 350 columns. Can someone point me in the right direction?
r dataframe dplyr data.table tidyr
I have a dataframe with all the columns being character like this.
ID <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
ToolID <- c("CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B",
"CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B")
Step <- c("Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F",
"Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F")
Measurement <- c("Length","Breadth","Width","Height",NA,NA,NA,NA,
"Length","Breadth","Width","Height",NA,NA,NA,NA)
Passfail <- c("Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass",
"Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass")
Points <- as.character(c(7,5,3,4,0,0,0,0,17,15,13,14,0,0,0,0))
Average <- as.character(c(7.5,6.5,7.1,6.6,NA,NA,NA,NA,17.5,16.5,17.1,16.6,NA,NA,NA,NA))
Sigma <- as.character(c(2.5,2.5,2.1,2.6,NA,NA,NA,NA,12.5,12.5,12.1,12.6,NA,NA,NA,NA))
Tool <- c("ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2",
"ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2")
Dose <- as.character(c(NA,NA,NA,NA,17.1,NA,NA,17.3,NA,NA,NA,NA,117.1,NA,NA,117.3))
Machine <- c("CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2",
"CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2")
df <- data.frame(ID,ToolID,Step,Measurement,Passfail,Points,Average,Sigma,Tool,Dose,Machine)
I am trying to check these character vectors for numeric values and then convert those with numeric values to numeric. I use the "varhandle" package in R to do it
library(varhandle)
if(all(check.numeric(df$Machine, na.rm=TRUE))){
# convert the vector to numeric
df$Machine <- as.numeric(df$Machine)
}
This works but is inefficient because I have to manually enter the column names like above. How can I do it more efficiently in a loop or use vectorization over multiple columns? My actual dataset has around 350 columns. Can someone point me in the right direction?
r dataframe dplyr data.table tidyr
r dataframe dplyr data.table tidyr
asked Oct 1 '18 at 13:54
SharathSharath
95411123
95411123
1
change <- sapply(dat, function(x) all(check.numeric(x, na.rm=TRUE))
and thendat[change] <- lapply(dat[change], as.numeric)
.
– lmo
Oct 1 '18 at 13:58
@Imo Works perfectly. Thank you
– Sharath
Oct 1 '18 at 14:01
Just usetype.convert
:type.convert(df)
.
– nicola
Oct 1 '18 at 14:09
1
@nicolatype.convert(df) ; # Error in type.convert(df) : the first argument must be of mode character
Am I missing something ?
– Ronak Shah
Oct 1 '18 at 14:10
1
Yes, thedata.frame
andlist
methods oftype.convert
have been introduced with R-3.5.0
– nicola
Oct 1 '18 at 14:16
|
show 2 more comments
1
change <- sapply(dat, function(x) all(check.numeric(x, na.rm=TRUE))
and thendat[change] <- lapply(dat[change], as.numeric)
.
– lmo
Oct 1 '18 at 13:58
@Imo Works perfectly. Thank you
– Sharath
Oct 1 '18 at 14:01
Just usetype.convert
:type.convert(df)
.
– nicola
Oct 1 '18 at 14:09
1
@nicolatype.convert(df) ; # Error in type.convert(df) : the first argument must be of mode character
Am I missing something ?
– Ronak Shah
Oct 1 '18 at 14:10
1
Yes, thedata.frame
andlist
methods oftype.convert
have been introduced with R-3.5.0
– nicola
Oct 1 '18 at 14:16
1
1
change <- sapply(dat, function(x) all(check.numeric(x, na.rm=TRUE))
and then dat[change] <- lapply(dat[change], as.numeric)
.– lmo
Oct 1 '18 at 13:58
change <- sapply(dat, function(x) all(check.numeric(x, na.rm=TRUE))
and then dat[change] <- lapply(dat[change], as.numeric)
.– lmo
Oct 1 '18 at 13:58
@Imo Works perfectly. Thank you
– Sharath
Oct 1 '18 at 14:01
@Imo Works perfectly. Thank you
– Sharath
Oct 1 '18 at 14:01
Just use
type.convert
: type.convert(df)
.– nicola
Oct 1 '18 at 14:09
Just use
type.convert
: type.convert(df)
.– nicola
Oct 1 '18 at 14:09
1
1
@nicola
type.convert(df) ; # Error in type.convert(df) : the first argument must be of mode character
Am I missing something ?– Ronak Shah
Oct 1 '18 at 14:10
@nicola
type.convert(df) ; # Error in type.convert(df) : the first argument must be of mode character
Am I missing something ?– Ronak Shah
Oct 1 '18 at 14:10
1
1
Yes, the
data.frame
and list
methods of type.convert
have been introduced with R-3.5.0– nicola
Oct 1 '18 at 14:16
Yes, the
data.frame
and list
methods of type.convert
have been introduced with R-3.5.0– nicola
Oct 1 '18 at 14:16
|
show 2 more comments
5 Answers
5
active
oldest
votes
We can use parse_guess
function from readr
package which basically tries to guess the type of columns.
library(readr)
library(dplyr)
df1 <- df %>% mutate_all(parse_guess)
str(df1)
#'data.frame': 16 obs. of 11 variables:
# $ ID : chr "A" "A" "A" "A" ...
# $ ToolID : chr "CCP_A" "CCP_A" "CCQ_A" "CCQ_A" ...
# $ Step : chr "Step_A" "Step_A" "Step_B" "Step_C" ...
# $ Measurement: chr "Length" "Breadth" "Width" "Height" ...
# $ Passfail : chr "Pass" "Pass" "Fail" "Fail" ...
# $ Points : int 7 5 3 4 0 0 0 0 17 15 ...
# $ Average : num 7.5 6.5 7.1 6.6 NA NA NA NA 17.5 16.5 ...
# $ Sigma : num 2.5 2.5 2.1 2.6 NA NA NA NA 12.5 12.5 ...
# $ Tool : chr "ABC_1" "ABC_2" "ABD_1" "ABD_2" ...
# $ Dose : num NA NA NA NA 17.1 NA NA 17.3 NA NA ...
# $ Machine : chr "CO2" "CO6" "CO3" "CO6" ...
add a comment |
We can do this in base R
df <- lapply(df, function(x) type.convert(as.character(x), as.is = TRUE))
str(df)
#'data.frame': 16 obs. of 11 variables:
# $ ID : chr "A" "A" "A" "A" ...
# $ ToolID : chr "CCP_A" "CCP_A" "CCQ_A" "CCQ_A" ...
# $ Step : chr "Step_A" "Step_A" "Step_B" "Step_C" ...
# $ Measurement: chr "Length" "Breadth" "Width" "Height" ...
# $ Passfail : chr "Pass" "Pass" "Fail" "Fail" ...
# $ Points : int 7 5 3 4 0 0 0 0 17 15 ...
# $ Average : num 7.5 6.5 7.1 6.6 NA NA NA NA 17.5 16.5 ...
# $ Sigma : num 2.5 2.5 2.1 2.6 NA NA NA NA 12.5 12.5 ...
# $ Tool : chr "ABC_1" "ABC_2" "ABD_1" "ABD_2" ...
# $ Dose : num NA NA NA NA 17.1 NA NA 17.3 NA NA ...
# $ Machine : chr "CO2" "CO6" "CO3" "CO6" ...
add a comment |
With varhandle and tidyverse :
df %>% mutate_if(purrr::compose(all,check.numeric),as.numeric)
add a comment |
I think that the easiest solution is to use all.is.numeric
from Hmisc
. Here's the simple example:
Hmisc::all.is.numeric(c("A", "B", "1"), what = "vector", extras = NA)
## [1] "A" "B" "1"
Hmisc::all.is.numeric(c("3", "2", "1", NA), what = "vector", extras = NA)
## [1] 3 2 1 NA
Then you can use mutate_all from dplyr
to do all the job for data.frame:
library(dplyr)
ID <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
ToolID <- c("CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B",
"CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B")
Step <- c("Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F",
"Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F")
Measurement <- c("Length","Breadth","Width","Height",NA,NA,NA,NA,
"Length","Breadth","Width","Height",NA,NA,NA,NA)
Passfail <- c("Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass",
"Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass")
Points <- as.character(c(7,5,3,4,0,0,0,0,17,15,13,14,0,0,0,0))
Average <- as.character(c(7.5,6.5,7.1,6.6,NA,NA,NA,NA,17.5,16.5,17.1,16.6,NA,NA,NA,NA))
Sigma <- as.character(c(2.5,2.5,2.1,2.6,NA,NA,NA,NA,12.5,12.5,12.1,12.6,NA,NA,NA,NA))
Tool <- c("ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2",
"ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2")
Dose <- as.character(c(NA,NA,NA,NA,17.1,NA,NA,17.3,NA,NA,NA,NA,117.1,NA,NA,117.3))
Machine <- c("CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2",
"CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2")
df <- data.frame(ID,ToolID,Step,Measurement,Passfail,Points,Average,Sigma,Tool,Dose,Machine)
dt2 <- df %>% mutate_all(function(x) Hmisc::all.is.numeric(x, what = "vector", extras = NA))
## check classes
sapply(dt2, class)
## ID ToolID Step Measurement Passfail Points
## "character" "character" "character" "character" "character" "numeric"
## Average Sigma Tool Dose Machine
## "numeric" "numeric" "character" "numeric" "character"
add a comment |
Another solution is retype from hablar package:
library(hablar)
df %>% retype()
which gives:
# A tibble: 16 x 11
ID ToolID Step Measurement Passfail Points Average Sigma Tool Dose Machine
<chr> <chr> <chr> <chr> <chr> <int> <dbl> <dbl> <chr> <dbl> <chr>
1 A CCP_A Step_A Length Pass 7 7.50 2.50 ABC_1 NA CO2
2 A CCP_A Step_A Breadth Pass 5 6.50 2.50 ABC_2 NA CO6
3 A CCQ_A Step_B Width Fail 3 7.10 2.10 ABD_1 NA CO3
4 A CCQ_A Step_C Height Fail 4 6.60 2.60 ABD_2 NA CO6
5 A IOT_B Step_D NA Pass 0 NA NA COB_1 17.1 CO2,CO6
6 A CCP_B Step_D NA Pass 0 NA NA COB_2 NA CO2,CO3,CO4
7 A CCQ_B Step_E NA Pass 0 NA NA COB_1 NA CO2,CO3
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52592719%2fcheck-if-all-values-are-numeric-over-multiple-columns-and-convert-them-to-numeri%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
We can use parse_guess
function from readr
package which basically tries to guess the type of columns.
library(readr)
library(dplyr)
df1 <- df %>% mutate_all(parse_guess)
str(df1)
#'data.frame': 16 obs. of 11 variables:
# $ ID : chr "A" "A" "A" "A" ...
# $ ToolID : chr "CCP_A" "CCP_A" "CCQ_A" "CCQ_A" ...
# $ Step : chr "Step_A" "Step_A" "Step_B" "Step_C" ...
# $ Measurement: chr "Length" "Breadth" "Width" "Height" ...
# $ Passfail : chr "Pass" "Pass" "Fail" "Fail" ...
# $ Points : int 7 5 3 4 0 0 0 0 17 15 ...
# $ Average : num 7.5 6.5 7.1 6.6 NA NA NA NA 17.5 16.5 ...
# $ Sigma : num 2.5 2.5 2.1 2.6 NA NA NA NA 12.5 12.5 ...
# $ Tool : chr "ABC_1" "ABC_2" "ABD_1" "ABD_2" ...
# $ Dose : num NA NA NA NA 17.1 NA NA 17.3 NA NA ...
# $ Machine : chr "CO2" "CO6" "CO3" "CO6" ...
add a comment |
We can use parse_guess
function from readr
package which basically tries to guess the type of columns.
library(readr)
library(dplyr)
df1 <- df %>% mutate_all(parse_guess)
str(df1)
#'data.frame': 16 obs. of 11 variables:
# $ ID : chr "A" "A" "A" "A" ...
# $ ToolID : chr "CCP_A" "CCP_A" "CCQ_A" "CCQ_A" ...
# $ Step : chr "Step_A" "Step_A" "Step_B" "Step_C" ...
# $ Measurement: chr "Length" "Breadth" "Width" "Height" ...
# $ Passfail : chr "Pass" "Pass" "Fail" "Fail" ...
# $ Points : int 7 5 3 4 0 0 0 0 17 15 ...
# $ Average : num 7.5 6.5 7.1 6.6 NA NA NA NA 17.5 16.5 ...
# $ Sigma : num 2.5 2.5 2.1 2.6 NA NA NA NA 12.5 12.5 ...
# $ Tool : chr "ABC_1" "ABC_2" "ABD_1" "ABD_2" ...
# $ Dose : num NA NA NA NA 17.1 NA NA 17.3 NA NA ...
# $ Machine : chr "CO2" "CO6" "CO3" "CO6" ...
add a comment |
We can use parse_guess
function from readr
package which basically tries to guess the type of columns.
library(readr)
library(dplyr)
df1 <- df %>% mutate_all(parse_guess)
str(df1)
#'data.frame': 16 obs. of 11 variables:
# $ ID : chr "A" "A" "A" "A" ...
# $ ToolID : chr "CCP_A" "CCP_A" "CCQ_A" "CCQ_A" ...
# $ Step : chr "Step_A" "Step_A" "Step_B" "Step_C" ...
# $ Measurement: chr "Length" "Breadth" "Width" "Height" ...
# $ Passfail : chr "Pass" "Pass" "Fail" "Fail" ...
# $ Points : int 7 5 3 4 0 0 0 0 17 15 ...
# $ Average : num 7.5 6.5 7.1 6.6 NA NA NA NA 17.5 16.5 ...
# $ Sigma : num 2.5 2.5 2.1 2.6 NA NA NA NA 12.5 12.5 ...
# $ Tool : chr "ABC_1" "ABC_2" "ABD_1" "ABD_2" ...
# $ Dose : num NA NA NA NA 17.1 NA NA 17.3 NA NA ...
# $ Machine : chr "CO2" "CO6" "CO3" "CO6" ...
We can use parse_guess
function from readr
package which basically tries to guess the type of columns.
library(readr)
library(dplyr)
df1 <- df %>% mutate_all(parse_guess)
str(df1)
#'data.frame': 16 obs. of 11 variables:
# $ ID : chr "A" "A" "A" "A" ...
# $ ToolID : chr "CCP_A" "CCP_A" "CCQ_A" "CCQ_A" ...
# $ Step : chr "Step_A" "Step_A" "Step_B" "Step_C" ...
# $ Measurement: chr "Length" "Breadth" "Width" "Height" ...
# $ Passfail : chr "Pass" "Pass" "Fail" "Fail" ...
# $ Points : int 7 5 3 4 0 0 0 0 17 15 ...
# $ Average : num 7.5 6.5 7.1 6.6 NA NA NA NA 17.5 16.5 ...
# $ Sigma : num 2.5 2.5 2.1 2.6 NA NA NA NA 12.5 12.5 ...
# $ Tool : chr "ABC_1" "ABC_2" "ABD_1" "ABD_2" ...
# $ Dose : num NA NA NA NA 17.1 NA NA 17.3 NA NA ...
# $ Machine : chr "CO2" "CO6" "CO3" "CO6" ...
answered Oct 1 '18 at 14:02
Ronak ShahRonak Shah
48.7k104370
48.7k104370
add a comment |
add a comment |
We can do this in base R
df <- lapply(df, function(x) type.convert(as.character(x), as.is = TRUE))
str(df)
#'data.frame': 16 obs. of 11 variables:
# $ ID : chr "A" "A" "A" "A" ...
# $ ToolID : chr "CCP_A" "CCP_A" "CCQ_A" "CCQ_A" ...
# $ Step : chr "Step_A" "Step_A" "Step_B" "Step_C" ...
# $ Measurement: chr "Length" "Breadth" "Width" "Height" ...
# $ Passfail : chr "Pass" "Pass" "Fail" "Fail" ...
# $ Points : int 7 5 3 4 0 0 0 0 17 15 ...
# $ Average : num 7.5 6.5 7.1 6.6 NA NA NA NA 17.5 16.5 ...
# $ Sigma : num 2.5 2.5 2.1 2.6 NA NA NA NA 12.5 12.5 ...
# $ Tool : chr "ABC_1" "ABC_2" "ABD_1" "ABD_2" ...
# $ Dose : num NA NA NA NA 17.1 NA NA 17.3 NA NA ...
# $ Machine : chr "CO2" "CO6" "CO3" "CO6" ...
add a comment |
We can do this in base R
df <- lapply(df, function(x) type.convert(as.character(x), as.is = TRUE))
str(df)
#'data.frame': 16 obs. of 11 variables:
# $ ID : chr "A" "A" "A" "A" ...
# $ ToolID : chr "CCP_A" "CCP_A" "CCQ_A" "CCQ_A" ...
# $ Step : chr "Step_A" "Step_A" "Step_B" "Step_C" ...
# $ Measurement: chr "Length" "Breadth" "Width" "Height" ...
# $ Passfail : chr "Pass" "Pass" "Fail" "Fail" ...
# $ Points : int 7 5 3 4 0 0 0 0 17 15 ...
# $ Average : num 7.5 6.5 7.1 6.6 NA NA NA NA 17.5 16.5 ...
# $ Sigma : num 2.5 2.5 2.1 2.6 NA NA NA NA 12.5 12.5 ...
# $ Tool : chr "ABC_1" "ABC_2" "ABD_1" "ABD_2" ...
# $ Dose : num NA NA NA NA 17.1 NA NA 17.3 NA NA ...
# $ Machine : chr "CO2" "CO6" "CO3" "CO6" ...
add a comment |
We can do this in base R
df <- lapply(df, function(x) type.convert(as.character(x), as.is = TRUE))
str(df)
#'data.frame': 16 obs. of 11 variables:
# $ ID : chr "A" "A" "A" "A" ...
# $ ToolID : chr "CCP_A" "CCP_A" "CCQ_A" "CCQ_A" ...
# $ Step : chr "Step_A" "Step_A" "Step_B" "Step_C" ...
# $ Measurement: chr "Length" "Breadth" "Width" "Height" ...
# $ Passfail : chr "Pass" "Pass" "Fail" "Fail" ...
# $ Points : int 7 5 3 4 0 0 0 0 17 15 ...
# $ Average : num 7.5 6.5 7.1 6.6 NA NA NA NA 17.5 16.5 ...
# $ Sigma : num 2.5 2.5 2.1 2.6 NA NA NA NA 12.5 12.5 ...
# $ Tool : chr "ABC_1" "ABC_2" "ABD_1" "ABD_2" ...
# $ Dose : num NA NA NA NA 17.1 NA NA 17.3 NA NA ...
# $ Machine : chr "CO2" "CO6" "CO3" "CO6" ...
We can do this in base R
df <- lapply(df, function(x) type.convert(as.character(x), as.is = TRUE))
str(df)
#'data.frame': 16 obs. of 11 variables:
# $ ID : chr "A" "A" "A" "A" ...
# $ ToolID : chr "CCP_A" "CCP_A" "CCQ_A" "CCQ_A" ...
# $ Step : chr "Step_A" "Step_A" "Step_B" "Step_C" ...
# $ Measurement: chr "Length" "Breadth" "Width" "Height" ...
# $ Passfail : chr "Pass" "Pass" "Fail" "Fail" ...
# $ Points : int 7 5 3 4 0 0 0 0 17 15 ...
# $ Average : num 7.5 6.5 7.1 6.6 NA NA NA NA 17.5 16.5 ...
# $ Sigma : num 2.5 2.5 2.1 2.6 NA NA NA NA 12.5 12.5 ...
# $ Tool : chr "ABC_1" "ABC_2" "ABD_1" "ABD_2" ...
# $ Dose : num NA NA NA NA 17.1 NA NA 17.3 NA NA ...
# $ Machine : chr "CO2" "CO6" "CO3" "CO6" ...
answered Oct 1 '18 at 15:20
akrunakrun
424k13209287
424k13209287
add a comment |
add a comment |
With varhandle and tidyverse :
df %>% mutate_if(purrr::compose(all,check.numeric),as.numeric)
add a comment |
With varhandle and tidyverse :
df %>% mutate_if(purrr::compose(all,check.numeric),as.numeric)
add a comment |
With varhandle and tidyverse :
df %>% mutate_if(purrr::compose(all,check.numeric),as.numeric)
With varhandle and tidyverse :
df %>% mutate_if(purrr::compose(all,check.numeric),as.numeric)
answered Oct 1 '18 at 14:05
Nicolas2Nicolas2
1,7631212
1,7631212
add a comment |
add a comment |
I think that the easiest solution is to use all.is.numeric
from Hmisc
. Here's the simple example:
Hmisc::all.is.numeric(c("A", "B", "1"), what = "vector", extras = NA)
## [1] "A" "B" "1"
Hmisc::all.is.numeric(c("3", "2", "1", NA), what = "vector", extras = NA)
## [1] 3 2 1 NA
Then you can use mutate_all from dplyr
to do all the job for data.frame:
library(dplyr)
ID <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
ToolID <- c("CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B",
"CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B")
Step <- c("Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F",
"Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F")
Measurement <- c("Length","Breadth","Width","Height",NA,NA,NA,NA,
"Length","Breadth","Width","Height",NA,NA,NA,NA)
Passfail <- c("Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass",
"Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass")
Points <- as.character(c(7,5,3,4,0,0,0,0,17,15,13,14,0,0,0,0))
Average <- as.character(c(7.5,6.5,7.1,6.6,NA,NA,NA,NA,17.5,16.5,17.1,16.6,NA,NA,NA,NA))
Sigma <- as.character(c(2.5,2.5,2.1,2.6,NA,NA,NA,NA,12.5,12.5,12.1,12.6,NA,NA,NA,NA))
Tool <- c("ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2",
"ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2")
Dose <- as.character(c(NA,NA,NA,NA,17.1,NA,NA,17.3,NA,NA,NA,NA,117.1,NA,NA,117.3))
Machine <- c("CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2",
"CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2")
df <- data.frame(ID,ToolID,Step,Measurement,Passfail,Points,Average,Sigma,Tool,Dose,Machine)
dt2 <- df %>% mutate_all(function(x) Hmisc::all.is.numeric(x, what = "vector", extras = NA))
## check classes
sapply(dt2, class)
## ID ToolID Step Measurement Passfail Points
## "character" "character" "character" "character" "character" "numeric"
## Average Sigma Tool Dose Machine
## "numeric" "numeric" "character" "numeric" "character"
add a comment |
I think that the easiest solution is to use all.is.numeric
from Hmisc
. Here's the simple example:
Hmisc::all.is.numeric(c("A", "B", "1"), what = "vector", extras = NA)
## [1] "A" "B" "1"
Hmisc::all.is.numeric(c("3", "2", "1", NA), what = "vector", extras = NA)
## [1] 3 2 1 NA
Then you can use mutate_all from dplyr
to do all the job for data.frame:
library(dplyr)
ID <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
ToolID <- c("CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B",
"CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B")
Step <- c("Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F",
"Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F")
Measurement <- c("Length","Breadth","Width","Height",NA,NA,NA,NA,
"Length","Breadth","Width","Height",NA,NA,NA,NA)
Passfail <- c("Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass",
"Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass")
Points <- as.character(c(7,5,3,4,0,0,0,0,17,15,13,14,0,0,0,0))
Average <- as.character(c(7.5,6.5,7.1,6.6,NA,NA,NA,NA,17.5,16.5,17.1,16.6,NA,NA,NA,NA))
Sigma <- as.character(c(2.5,2.5,2.1,2.6,NA,NA,NA,NA,12.5,12.5,12.1,12.6,NA,NA,NA,NA))
Tool <- c("ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2",
"ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2")
Dose <- as.character(c(NA,NA,NA,NA,17.1,NA,NA,17.3,NA,NA,NA,NA,117.1,NA,NA,117.3))
Machine <- c("CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2",
"CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2")
df <- data.frame(ID,ToolID,Step,Measurement,Passfail,Points,Average,Sigma,Tool,Dose,Machine)
dt2 <- df %>% mutate_all(function(x) Hmisc::all.is.numeric(x, what = "vector", extras = NA))
## check classes
sapply(dt2, class)
## ID ToolID Step Measurement Passfail Points
## "character" "character" "character" "character" "character" "numeric"
## Average Sigma Tool Dose Machine
## "numeric" "numeric" "character" "numeric" "character"
add a comment |
I think that the easiest solution is to use all.is.numeric
from Hmisc
. Here's the simple example:
Hmisc::all.is.numeric(c("A", "B", "1"), what = "vector", extras = NA)
## [1] "A" "B" "1"
Hmisc::all.is.numeric(c("3", "2", "1", NA), what = "vector", extras = NA)
## [1] 3 2 1 NA
Then you can use mutate_all from dplyr
to do all the job for data.frame:
library(dplyr)
ID <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
ToolID <- c("CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B",
"CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B")
Step <- c("Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F",
"Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F")
Measurement <- c("Length","Breadth","Width","Height",NA,NA,NA,NA,
"Length","Breadth","Width","Height",NA,NA,NA,NA)
Passfail <- c("Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass",
"Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass")
Points <- as.character(c(7,5,3,4,0,0,0,0,17,15,13,14,0,0,0,0))
Average <- as.character(c(7.5,6.5,7.1,6.6,NA,NA,NA,NA,17.5,16.5,17.1,16.6,NA,NA,NA,NA))
Sigma <- as.character(c(2.5,2.5,2.1,2.6,NA,NA,NA,NA,12.5,12.5,12.1,12.6,NA,NA,NA,NA))
Tool <- c("ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2",
"ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2")
Dose <- as.character(c(NA,NA,NA,NA,17.1,NA,NA,17.3,NA,NA,NA,NA,117.1,NA,NA,117.3))
Machine <- c("CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2",
"CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2")
df <- data.frame(ID,ToolID,Step,Measurement,Passfail,Points,Average,Sigma,Tool,Dose,Machine)
dt2 <- df %>% mutate_all(function(x) Hmisc::all.is.numeric(x, what = "vector", extras = NA))
## check classes
sapply(dt2, class)
## ID ToolID Step Measurement Passfail Points
## "character" "character" "character" "character" "character" "numeric"
## Average Sigma Tool Dose Machine
## "numeric" "numeric" "character" "numeric" "character"
I think that the easiest solution is to use all.is.numeric
from Hmisc
. Here's the simple example:
Hmisc::all.is.numeric(c("A", "B", "1"), what = "vector", extras = NA)
## [1] "A" "B" "1"
Hmisc::all.is.numeric(c("3", "2", "1", NA), what = "vector", extras = NA)
## [1] 3 2 1 NA
Then you can use mutate_all from dplyr
to do all the job for data.frame:
library(dplyr)
ID <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
ToolID <- c("CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B",
"CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B")
Step <- c("Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F",
"Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F")
Measurement <- c("Length","Breadth","Width","Height",NA,NA,NA,NA,
"Length","Breadth","Width","Height",NA,NA,NA,NA)
Passfail <- c("Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass",
"Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass")
Points <- as.character(c(7,5,3,4,0,0,0,0,17,15,13,14,0,0,0,0))
Average <- as.character(c(7.5,6.5,7.1,6.6,NA,NA,NA,NA,17.5,16.5,17.1,16.6,NA,NA,NA,NA))
Sigma <- as.character(c(2.5,2.5,2.1,2.6,NA,NA,NA,NA,12.5,12.5,12.1,12.6,NA,NA,NA,NA))
Tool <- c("ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2",
"ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2")
Dose <- as.character(c(NA,NA,NA,NA,17.1,NA,NA,17.3,NA,NA,NA,NA,117.1,NA,NA,117.3))
Machine <- c("CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2",
"CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2")
df <- data.frame(ID,ToolID,Step,Measurement,Passfail,Points,Average,Sigma,Tool,Dose,Machine)
dt2 <- df %>% mutate_all(function(x) Hmisc::all.is.numeric(x, what = "vector", extras = NA))
## check classes
sapply(dt2, class)
## ID ToolID Step Measurement Passfail Points
## "character" "character" "character" "character" "character" "numeric"
## Average Sigma Tool Dose Machine
## "numeric" "numeric" "character" "numeric" "character"
answered Oct 1 '18 at 14:11
Zygmunt ZawadzkiZygmunt Zawadzki
2115
2115
add a comment |
add a comment |
Another solution is retype from hablar package:
library(hablar)
df %>% retype()
which gives:
# A tibble: 16 x 11
ID ToolID Step Measurement Passfail Points Average Sigma Tool Dose Machine
<chr> <chr> <chr> <chr> <chr> <int> <dbl> <dbl> <chr> <dbl> <chr>
1 A CCP_A Step_A Length Pass 7 7.50 2.50 ABC_1 NA CO2
2 A CCP_A Step_A Breadth Pass 5 6.50 2.50 ABC_2 NA CO6
3 A CCQ_A Step_B Width Fail 3 7.10 2.10 ABD_1 NA CO3
4 A CCQ_A Step_C Height Fail 4 6.60 2.60 ABD_2 NA CO6
5 A IOT_B Step_D NA Pass 0 NA NA COB_1 17.1 CO2,CO6
6 A CCP_B Step_D NA Pass 0 NA NA COB_2 NA CO2,CO3,CO4
7 A CCQ_B Step_E NA Pass 0 NA NA COB_1 NA CO2,CO3
add a comment |
Another solution is retype from hablar package:
library(hablar)
df %>% retype()
which gives:
# A tibble: 16 x 11
ID ToolID Step Measurement Passfail Points Average Sigma Tool Dose Machine
<chr> <chr> <chr> <chr> <chr> <int> <dbl> <dbl> <chr> <dbl> <chr>
1 A CCP_A Step_A Length Pass 7 7.50 2.50 ABC_1 NA CO2
2 A CCP_A Step_A Breadth Pass 5 6.50 2.50 ABC_2 NA CO6
3 A CCQ_A Step_B Width Fail 3 7.10 2.10 ABD_1 NA CO3
4 A CCQ_A Step_C Height Fail 4 6.60 2.60 ABD_2 NA CO6
5 A IOT_B Step_D NA Pass 0 NA NA COB_1 17.1 CO2,CO6
6 A CCP_B Step_D NA Pass 0 NA NA COB_2 NA CO2,CO3,CO4
7 A CCQ_B Step_E NA Pass 0 NA NA COB_1 NA CO2,CO3
add a comment |
Another solution is retype from hablar package:
library(hablar)
df %>% retype()
which gives:
# A tibble: 16 x 11
ID ToolID Step Measurement Passfail Points Average Sigma Tool Dose Machine
<chr> <chr> <chr> <chr> <chr> <int> <dbl> <dbl> <chr> <dbl> <chr>
1 A CCP_A Step_A Length Pass 7 7.50 2.50 ABC_1 NA CO2
2 A CCP_A Step_A Breadth Pass 5 6.50 2.50 ABC_2 NA CO6
3 A CCQ_A Step_B Width Fail 3 7.10 2.10 ABD_1 NA CO3
4 A CCQ_A Step_C Height Fail 4 6.60 2.60 ABD_2 NA CO6
5 A IOT_B Step_D NA Pass 0 NA NA COB_1 17.1 CO2,CO6
6 A CCP_B Step_D NA Pass 0 NA NA COB_2 NA CO2,CO3,CO4
7 A CCQ_B Step_E NA Pass 0 NA NA COB_1 NA CO2,CO3
Another solution is retype from hablar package:
library(hablar)
df %>% retype()
which gives:
# A tibble: 16 x 11
ID ToolID Step Measurement Passfail Points Average Sigma Tool Dose Machine
<chr> <chr> <chr> <chr> <chr> <int> <dbl> <dbl> <chr> <dbl> <chr>
1 A CCP_A Step_A Length Pass 7 7.50 2.50 ABC_1 NA CO2
2 A CCP_A Step_A Breadth Pass 5 6.50 2.50 ABC_2 NA CO6
3 A CCQ_A Step_B Width Fail 3 7.10 2.10 ABD_1 NA CO3
4 A CCQ_A Step_C Height Fail 4 6.60 2.60 ABD_2 NA CO6
5 A IOT_B Step_D NA Pass 0 NA NA COB_1 17.1 CO2,CO6
6 A CCP_B Step_D NA Pass 0 NA NA COB_2 NA CO2,CO3,CO4
7 A CCQ_B Step_E NA Pass 0 NA NA COB_1 NA CO2,CO3
answered Oct 31 '18 at 15:33
davsjobdavsjob
80747
80747
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52592719%2fcheck-if-all-values-are-numeric-over-multiple-columns-and-convert-them-to-numeri%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
change <- sapply(dat, function(x) all(check.numeric(x, na.rm=TRUE))
and thendat[change] <- lapply(dat[change], as.numeric)
.– lmo
Oct 1 '18 at 13:58
@Imo Works perfectly. Thank you
– Sharath
Oct 1 '18 at 14:01
Just use
type.convert
:type.convert(df)
.– nicola
Oct 1 '18 at 14:09
1
@nicola
type.convert(df) ; # Error in type.convert(df) : the first argument must be of mode character
Am I missing something ?– Ronak Shah
Oct 1 '18 at 14:10
1
Yes, the
data.frame
andlist
methods oftype.convert
have been introduced with R-3.5.0– nicola
Oct 1 '18 at 14:16