na

mean( ,na.rm=TRUE) still returns NA

岁酱吖の 提交于 2020-08-22 19:08:50
问题 I'm very new to R (moving over from SPSS). I'm using RStudio on a Mac running Mavericks. Please answer my question in words of 2 syllables as this is my first real attempt at anything like this. I've worked through some basic tutorials and can make things work on all the sample data. I have a data set with 64,000-ish rows and about 20 columns. I want to get the mean of the variable "hold_time", but whatever I try I get either NA or NA and a warning message I have tried all of the following: >

replace na in a dataframe with value in other df r [duplicate]

荒凉一梦 提交于 2020-08-17 05:56:29
问题 This question already has answers here : Can I replace NAs when joining two data frames with dplyr? (2 answers) Closed 19 days ago . This is an example of my df: x<-tibble::tribble( ~ID, ~Month, ~Value, "A", 1L, 100L, "A", 2L, 200L, "A", 3L, NA, "A", 4L, 400L, "B", 1L, 50L, "B", 2L, 20L, "B", 3L, 30L, "B", 4L, NA, "C", 1L, NA, "C", 2L, 60L, "C", 3L, 70L, "C", 4L, 60L, "D", 1L, 60L, "D", 2L, 60L, "D", 3L, 60L, "D", 4L, 50L ) And I have another df with this values: y<-tibble::tribble( ~Month,

Return last data frame column which is not NA

ぃ、小莉子 提交于 2020-08-07 05:55:47
问题 I have a dataset consisting of multiple cases that are stamped either 1 OR NA. I'm trying to figure out a way to return the highest numbered stamp that is not NA for each case. Here are some sample data: PIN <- c("case1", "case2", "case3", "case4", "case5") STAMP_1 <- c(1, 1, 1, 1, 1) STAMP_2 <- c(NA, 1, 1, NA, 1) STAMP_3 <- c(1, NA, 1, 1, NA) STAMP_4 <- c(NA, NA, 1, 1, NA) STAMP_5 <- c(1, NA, NA, 1, NA) data <- data.frame(PIN, STAMP_1, STAMP_2, STAMP_3, STAMP_4, STAMP_5) I'd like to figure

Return last data frame column which is not NA

妖精的绣舞 提交于 2020-08-07 05:53:53
问题 I have a dataset consisting of multiple cases that are stamped either 1 OR NA. I'm trying to figure out a way to return the highest numbered stamp that is not NA for each case. Here are some sample data: PIN <- c("case1", "case2", "case3", "case4", "case5") STAMP_1 <- c(1, 1, 1, 1, 1) STAMP_2 <- c(NA, 1, 1, NA, 1) STAMP_3 <- c(1, NA, 1, 1, NA) STAMP_4 <- c(NA, NA, 1, 1, NA) STAMP_5 <- c(1, NA, NA, 1, NA) data <- data.frame(PIN, STAMP_1, STAMP_2, STAMP_3, STAMP_4, STAMP_5) I'd like to figure

If values in a range of columns aren't present in another column, replace with NA

元气小坏坏 提交于 2020-07-22 21:33:39
问题 I have a dataset that includes some non-referenced data that I would like to replace with NA. In the following example, if the data in columns rep1 to rep4 does not match one of the values in the ID column, I would like to replace the value with NA. In this case, the values of x, y, and z aren't listed in the ID column, so they should be replaced. This is a somewhat similar question that I asked earlier here : If data present, replace with data from another column based on row ID I think the

If values in a range of columns aren't present in another column, replace with NA

这一生的挚爱 提交于 2020-07-22 21:33:18
问题 I have a dataset that includes some non-referenced data that I would like to replace with NA. In the following example, if the data in columns rep1 to rep4 does not match one of the values in the ID column, I would like to replace the value with NA. In this case, the values of x, y, and z aren't listed in the ID column, so they should be replaced. This is a somewhat similar question that I asked earlier here : If data present, replace with data from another column based on row ID I think the

xtabs counts with NA and na.action = na.pass in R

橙三吉。 提交于 2020-06-28 06:22:32
问题 I have the following data.frame : x <- data.frame(A = c("Y", "Y", "Z", NA), B = c(NA, TRUE, FALSE, TRUE), C = c(TRUE, TRUE, NA, FALSE)) I need to compute the following table: A B C Y 1 2 Z 0 0 <NA> 1 0 However I am unable to achieve this result with xtabs , even with na.action = na.pass : xtabs(formula = cbind(B, C) ~ A, data = x, addNA = TRUE, na.action = na.pass) A B C Y 2 Z 0 <NA> 1 0 From ?xtabs : na.action a function which indicates what should happen when the data contain NAs. If

Inconsistency of na.action between xtabs and aggregate in R

本小妞迷上赌 提交于 2020-06-27 09:15:58
问题 I have the following data.frame: x <- data.frame(A = c("Y", "Y", "Z", NA), B = c(NA, TRUE, FALSE, TRUE), C = c(TRUE, TRUE, NA, FALSE)) And I need to compute the following table with xtabs : A B C Y 1 2 Z 0 0 <NA> 1 0 I was told to use na.action = NULL, which indeed returns the table I need: xtabs(formula = cbind(B, C) ~ A, data = x, addNA = TRUE, na.action = NULL) A B C Y 1 2 Z 0 0 <NA> 1 0 However, na.action = na.pass returns a different table: xtabs(formula = cbind(B, C) ~ A, data = x,

Inconsistency of na.action between xtabs and aggregate in R

旧时模样 提交于 2020-06-27 09:15:08
问题 I have the following data.frame: x <- data.frame(A = c("Y", "Y", "Z", NA), B = c(NA, TRUE, FALSE, TRUE), C = c(TRUE, TRUE, NA, FALSE)) And I need to compute the following table with xtabs : A B C Y 1 2 Z 0 0 <NA> 1 0 I was told to use na.action = NULL, which indeed returns the table I need: xtabs(formula = cbind(B, C) ~ A, data = x, addNA = TRUE, na.action = NULL) A B C Y 1 2 Z 0 0 <NA> 1 0 However, na.action = na.pass returns a different table: xtabs(formula = cbind(B, C) ~ A, data = x,

Filtering rows in R unexpectedly removes NAs when using subset or dplyr::filter

扶醉桌前 提交于 2020-06-08 17:45:59
问题 I have a dataset df and I would like to remove all rows for which variable y does not have the value a . Variable y also contains some NAs : df <- data.frame(x=1:3, y=c('a', NA, 'c')) I can achieve this using R's indexing syntax like this: df[df$y!='a',] x y 2 <NA> 3 c Note this returns both the NA and the value c - which is what I want. However, when I try the same thing using subset or dplyr::filter , the NA gets stripped out: subset(df, y!='a') x y 3 c dplyr::filter(df, y!='a') x y 3 c Why