na

Is it possible to set na.rm to TRUE globally?

≡放荡痞女 提交于 2019-11-27 02:35:50
问题 For commands like max the option na.rm is set by default to FALSE . I understand why this is a good idea in general, but I'd like to turn it off reversibly for a while -- i.e. during a session. How can I require R to set na.rm = TRUE whenever it is an option? I found options(na.action = na.omit) but this doesn't work. I know that I can set a na.rm=TRUE option for each and every function I write. my.max <- function(x) {max(x, na.rm=TRUE)} But that's not what I am looking for. I'm wondering if

Hiding NA's when printing a dataframe in knitr

元气小坏坏 提交于 2019-11-27 02:09:08
问题 I'm trying to print a table in knitr from a data frame using xtable . The table in the example below has the dimensions 3x7 but the third row only has one value, in the second column. The rest of the cells in the third row are 'NA'. When I compile the document, is there a way to prevent knitr from printing the NA's in the third row, so instead of NA I just have blank space? It feels like this should be a simple solution but I can't work out where/how to hide the NA's. Is it a change I need to

Find names of columns which contain missing values

落爺英雄遲暮 提交于 2019-11-27 02:03:25
问题 I want to find all the names of columns with NA or missing data and store these column names in a vector. # create matrix a <- c(1,2,3,4,5,NA,7,8,9,10,NA,12,13,14,NA,16,17,18,19,20) cnames <- c("aa", "bb", "cc", "dd", "ee") mymatrix <- matrix(a, nrow = 4, ncol = 5, byrow = TRUE) colnames(mymatrix) <- cnames mymatrix # aa bb cc dd ee # [1,] 1 2 3 4 5 # [2,] NA 7 8 9 10 # [3,] NA 12 13 14 NA # [4,] 16 17 18 19 20 The desired result: columns "aa" and "ee" . My attempt: bad <- character() for (j

How to subset data in R without losing NA rows?

谁都会走 提交于 2019-11-27 02:02:50
I have some data that I am looking at in R. One particular column, titled "Height", contains a few rows of NA. I am looking to subset my data-frame so that all Heights above a certain value are excluded from my analysis. df2 <- subset ( df1 , Height < 40 ) However whenever I do this, R automatically removes all rows that contain NA values for Height. I do not want this. I have tried including arguments for na.rm f1 <- function ( x , na.rm = FALSE ) { df2 <- subset ( x , Height < 40 ) } f1 ( df1 , na.rm = FALSE ) but this does not seem to do anything; the rows with NA still end up disappearing

Setting <NA> to blank

半腔热情 提交于 2019-11-27 01:30:26
问题 I have a dataframe with an NA row: df = data.frame(c("classA", NA, "classB"), t(data.frame(rep("A", 5), rep(NA, 5), rep("B", 5)))) rownames(df) <- c(1,2,3) colnames(df) <- c("class", paste("Year", 1:5, sep = "")) > df class Year1 Year2 Year3 Year4 Year5 1 classA A A A A A 2 <NA> <NA> <NA> <NA> <NA> <NA> 3 classB B B B B B I introduced the empty row (NA row) on purpose because I wanted to have some space between classA row and classB row. Now, I would like to substitute the <NA> by blank, so

How to avoid warning when introducing NAs by coercion

血红的双手。 提交于 2019-11-26 23:54:20
I generally prefer to code R so that I don't get warnings, but I don't know how to avoid getting a warning when using as.numeric to convert a character vector. For example: x <- as.numeric(c("1", "2", "X")) Will give me a warning because it introduced NAs by coercion. I want NAs introduced by coercion - is there a way to tell it "yes this is what I want to do". Or should I just live with the warning? Or should I be using a different function for this task? Use suppressWarnings() : suppressWarnings(as.numeric(c("1", "2", "X"))) [1] 1 2 NA This suppresses warnings. suppressWarnings() has already

Replace NA with previous and next rows mean in R

北城以北 提交于 2019-11-26 23:23:35
问题 How could I Replace a NA with mean of its previous and next rows in a fast manner? name grade 1 A 56 2 B NA 3 C 70 4 D 96 such that B's grade would be 63. 回答1: Or you may try na.approx from package zoo : "Missing values (NAs) are replaced by linear interpolation" library(zoo) x <- c(56, NA, 70, 96) na.approx(x) # [1] 56 63 70 96 This also works if you have more than one consecutive NA : vals <- c(1, NA, NA, 7, NA, 10) na.approx(vals) # [1] 1.0 3.0 5.0 7.0 8.5 10.0 na.approx is based on the

Counting non NAs in a data frame; getting answer as a vector

拥有回忆 提交于 2019-11-26 22:45:47
问题 Say I have the following R data.frame ZZZ : ( ZZZ <- structure(list(n = c(1, 2, NA), m = c(6, NA, NA), o = c(7, 8, 8)), .Names = c("n", "m", "o"), row.names = c(NA, -3L), class = "data.frame") ) ## not run n m o 1 1 6 7 2 2 NA 8 3 NA NA 8 I want to know, in the form of a vector, how many non-NAs I've got. I want the answer available to me as: 2, 1, 3 When I use the command length(ZZZ) , I get 3 , which of course is the number of vectors in the data.frame , a valuable enough piece of

Add a box for the NA values to the ggplot legend for a continous map

限于喜欢 提交于 2019-11-26 21:50:19
问题 I have got a map with a legend gradient and I would like to add a box for the NA values. My question is really similar to this one and this one. Also I have read this topic, but I can't find a "nice" solution somewhere or maybe there isn't any? Here is an reproducible example: library(ggplot2) map <- map_data("world") map$value <- setNames(sample(-50:50, length(unique(map$region)), TRUE), unique(map$region))[map$region] map[map$region == "Russia", "value"] <- NA ggplot() + geom_polygon(data =

Combining more than 2 columns by removing NA's in R

喜欢而已 提交于 2019-11-26 21:23:59
问题 At first sight this seems a duplicate of Combine/merge columns while avoiding NA? but in fact it isn't. I am dealing sometimes with more than two columns instead of just two. My dataframe looks like this: col1 col2 col3 col4 col5 [1,] 1 NA NA 13 NA [2,] NA NA 10 NA 18 [3,] NA 7 NA 15 NA [4,] 4 NA NA 16 NA Now I want to "collapse" this dataframe into a dataframe with less columns and with removed NA's. In fact I am looking for and "excel way of doing": removing one cell and the whole row will