-2

I have a data set which has null values in between, I wish to know on a general note, I want to treat these null values as a common character value without assigning or hard coding any value in the data, such that the null too is considered a variable. E.g: Say there are two vectors:

a1 <- c("a1","a2","","a3")
a2 <- c("b1","b2","b3","b4")
a12 <- data.frame(a1,a2)

Basically, I have a much larger data, considering all blank values as "na" without assigning any value to the blank fields.

James Z
  • 12,209
  • 10
  • 24
  • 44
Robert J
  • 79
  • 6
  • 2
    Could you please tell us what you want to do with the data frame – Bertil Baron Dec 22 '17 at 13:19
  • As a sidenote on the names of empty elements: be careful that a `NULL` value is different from an empty string `''`: `identical(c('a','b',NULL),c('a','b')) # --> TRUE` so a NULL value really is nothing, not like an empty string `''` or a missing value `NA`. – akraf Dec 22 '17 at 13:28
  • @BertilBaron, see I have two columns with a1 and a2, the issue is that "a1" has na values. when I plot a bar chart, "na" introduces an error in the chart, however hard coding the "na" values with variable "na" does the job, I want to use the blank values as a common variable say "na" at the back end and not modify the dataset. – Robert J Dec 22 '17 at 13:30
  • @BertilBaron, Hi I kind of solved this issue, I am basically looking to create traces in eventlog datasets where null values create issue. Please help me with this post as I am struggling to find a fix, https://stackoverflow.com/questions/47951307/selection-of-activity-trace-in-a-chart-and-display-in-a-data-table-in-r-shiny – Robert J Dec 23 '17 at 10:55
  • If any of the solutions provided here, please select one post to accept it as the answer. See this post to learn how to do it: https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – www Dec 26 '17 at 13:59
  • @www, I got it answered from someone. Thanks a lot, however I appreciate your effort for helping me with this post, kindly check my requirement and suggest, Thank a lot. https://stackoverflow.com/questions/47976994/arranging-bars-of-a-bar-plot-with-null-in-descending-order-in-r/47979583#47979583 – Robert J Dec 26 '17 at 16:14
  • @RobertJ On StackOverflow, it is a general practice to accept an answer when people answered your question with valid solutions. This will show future viewers this question has been solved. If you don't do that, I don't see why people want to answer your question. – www Dec 26 '17 at 16:32
  • @www, sure I'll do that, I appreciate the community's effort in helping me with the requirement. Also, kindly help me with that post, that null issue has made me struggle for ages now. – Robert J Dec 26 '17 at 16:37

3 Answers3

3

R base does the job. Try a12[a12 == ""] <- NA

a12
#    a1 a2
#1   a1 b1
#2   a2 b2
#3 <NA> b3
#4   a3 b4
nghauran
  • 6,648
  • 2
  • 20
  • 29
  • thanks for replying, See can you help me here, I don't wish to assign NA in the data and modify it, just consider a blank value as a common variable in the backend. the end user should see a blank value only. – Robert J Dec 22 '17 at 13:38
  • If I got you right, you don't need to set blank values as `NA`, no? What do you want to do exactly? – nghauran Dec 22 '17 at 13:56
  • If this is just about printing, you could create a `class` and an associated `print` method – MichaelChirico Dec 22 '17 at 13:57
  • I want to know that is there a way, I can consider these blank values to be "NA" without actually hard coding them. – Robert J Dec 22 '17 at 14:03
  • That's what `a12[a12 == ""] <- NA` does. The `NA` you see in R is just a missing value indicator - which is different than a blank cell. Check the result of this test `is.na("") == is.na(NA)`. Export your data to an Excel file by using `xlsx::write.xlsx(a12, "a12.xlsx", showNA = FALSE)` and you can see that `NA` are represented as blank cells in Excel. – nghauran Dec 22 '17 at 14:21
  • @MichaelChirico, I'll check into this, however I kind of figured the solution myself, if I use stringasfactors = F, i don't have to declare the hard code value and I can get the job done, thanks everyone for the support. – Robert J Dec 23 '17 at 10:19
  • @MichaelChirico, I require your assistance with this another post in which I need help to tweak the logic a little, kindly check https://stackoverflow.com/questions/47951307/selection-of-activity-trace-in-a-chart-and-display-in-a-data-table-in-r-shiny – Robert J Dec 23 '17 at 10:20
  • @ANG, kindly help me here with this post. – Robert J Dec 27 '17 at 07:10
1

Another possibility is to use is.na<- together with a (logical) index vector.

is.na(a12) <- a12 == ""
a12
#    a1 a2
#1   a1 b1
#2   a2 b2
#3 <NA> b3
#4   a3 b4

Note that a numeric index vector, which(a12 == "") would also do the job.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • I figured this out, thanks a lot for your help,kindly help me with this script, I have some issue with displaying table details here https://stackoverflow.com/questions/47951307/selection-of-activity-trace-in-a-chart-and-display-in-a-data-table-in-r-shiny?noredirect=1#comment82886535_47951307 – Robert J Dec 24 '17 at 07:41
0

dplyr solution:

Temporarily assign empty strings the NA value inside a pipe:

a12%>% mutate_all(. %>% ifelse(. == '',NA,.)) %>% ....

(or the same, a bit longer but more conventional syntax:

a12%>% mutate_all(function(x) ifelse(x == '', NA, x)) %>% ....

After this, coupled by further %>% ... you might do something with your data

akraf
  • 2,965
  • 20
  • 44
  • kindly help me with this post, I am not sure how to handle null values here. https://stackoverflow.com/questions/47976994/arranging-bars-of-a-bar-plot-with-null-in-descending-order-in-r?noredirect=1#comment82923110_47976994 – Robert J Dec 26 '17 at 10:49