Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
datautils
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Environments
Packages & Registries
Packages & Registries
Package Registry
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bioinfo
datautils
Commits
1ff1993d
Commit
1ff1993d
authored
Mar 10, 2016
by
Holger Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed empty_as_na for factors
parent
551b6168
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
12 deletions
+38
-12
R/core_commons.R
R/core_commons.R
+38
-12
No files found.
R/core_commons.R
View file @
1ff1993d
...
...
@@ -186,23 +186,49 @@ replaceNA <- function(x, withValue) { x[is.na(x)] <- withValue; x }
## todo still needed
safe_ifelse
<-
function
(
cond
,
yes
,
no
)
{
class.y
<-
class
(
yes
)
if
(
"factor"
%in%
class.y
)
{
# Note the small condition change here
levels.y
=
levels
(
yes
)
}
X
<-
ifelse
(
cond
,
yes
,
no
)
if
(
"factor"
%in%
class.y
)
{
# Note the small condition change here
X
=
as.factor
(
X
)
levels
(
X
)
=
levels.y
}
else
{
class
(
X
)
<-
class.y
#browser()
isfacOrChar
<-
function
(
x
)
class
(
x
)
%in%
c
(
"factor"
,
"character"
)
if
(
isfacOrChar
(
yes
)
|
isfacOrChar
(
no
))
{
yes
<-
ac
(
yes
)
no
<-
ac
(
no
)
}
return
(
X
)
ifelse
(
cond
,
yes
,
no
)
}
## for na instead use mutate_each with:
empty_as_na
<-
function
(
x
)
safe_ifelse
(
x
==
""
,
NA
,
x
)
#empty_as_na <- function(x) safe_ifelse(x=="", NA, x)
#empty_as_na <- function(x) ifelse(class(x) %in% c("factor", "character") & x=="", NA, x)
empty_as_na
<-
function
(
x
){
if
(
"factor"
%in%
class
(
x
))
x
<-
as.character
(
x
)
## since ifelse wont work with factors
ifelse
(
as.character
(
x
)
!=
""
,
x
,
NA
)
}
#if(F){ ## DEBUG of empty_as_na
#cond <- allJobs %>% head %$% submit_time %>% c("")
#empty_as_na( cond)
#cond <- allJobs %>% head %$% resubmission_of
#empty_as_na( cond)
#
#empty_as_na( c(1, 2, NA))
#empty_as_na( c("sdf", "sdf2", NA))
#empty_as_na( c("sdf", "sdf2", ""))
#
#myFac <- as.factor(c("sdf", "sdf2", NA))
#empty_as_na( myFac)
#ifelse(as.character(myFac)!="", myFac, NA)
#
#empty_as_na( c("sdf", "sdf2", ""))
#
#iris[1,1] <- ""
#apply(iris, 2, function(x) gsub("^$|^ $", NA, x))
#}
## see http://stackoverflow.com/questions/24172111/change-the-blank-cells-to-na/33952598#33952598
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment