Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201943

Use ifelse and is.null to test for NULL values

$
0
0

I have a data frame:

df <- structure(list(gene = structure(1:6, .Label = c("128up", "14-3-3epsilon", 
"14-3-3zeta", "140up", "18SrRNA-Psi:CR41602", "18SrRNA-Psi:CR45861"
), class = "factor"), fpkm = list(NULL, 0.4, NA_real_, NULL, 
    NULL, NULL)), .Names = c("gene", "fpkm"), row.names = c(NA, 
6L), class = "data.frame")

                 gene fpkm
1               128up NULL
2       14-3-3epsilon  0.4
3          14-3-3zeta   NA
4               140up NULL
5 18SrRNA-Psi:CR41602 NULL
6 18SrRNA-Psi:CR45861 NULL

I would like to add a new column, level based on the value in fpkm. In cases where the value is NULL or NA I would like the value to be 'not_expressed, elseexpressed`.

I'm using mutate to achieve this, for NA and NULL values separately, but this does not have the desired effect on NULL values:

mutate(df, level = ifelse(is.na(fpkm), 'not_expressed' , 'expressed'))

                 gene fpkm         level
1               128up NULL     expressed
2       14-3-3epsilon  0.4     expressed
3          14-3-3zeta   NA not_expressed # Expected
4               140up NULL     expressed
5 18SrRNA-Psi:CR41602 NULL     expressed
6 18SrRNA-Psi:CR45861 NULL     expressed

  mutate(df, level = ifelse(is.null(fpkm), 'not_expressed' , 'expressed'))

                 gene fpkm     level
1               128up NULL expressed
2       14-3-3epsilon  0.4 expressed
3          14-3-3zeta   NA expressed
4               140up NULL expressed
5 18SrRNA-Psi:CR41602 NULL expressed
6 18SrRNA-Psi:CR45861 NULL expressed

I can't work out why this isn't working - is.null(unlist(test$fpkm[1])) returns TRUE

I've also tried: ifelse(is.null(df$fpkm), 'not_expressed', 'expressed')

and: ifelse(is.null(unlist(df$fpkm)), 'not_expressed', 'expressed')

...neither of which work


Viewing all articles
Browse latest Browse all 201943

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>