na

R: Adding NAs into Data Frame

不想你离开。 提交于 2019-11-28 13:39:29
I have a data frame like so: Name Position Value a 1 0.2 a 3 0.4 a 4 0.3 b 1 0.5 b 2 0.4 b 5 0.3 c 2 0.3 c 3 0.4 c 5 0.1 d 1 0.2 d 2 0.4 d 3 0.5 I want to make it so that Position always go from 1 to 5 for each Name and fill in NAs into Value like so: Name Position Value a 1 0.2 a 2 NA a 3 0.4 a 4 0.3 a 5 NA b 1 0.5 b 2 0.4 b 3 NA b 4 NA b 5 0.3 c 1 NA c 2 0.3 c 3 0.4 c 4 NA c 5 0.1 d 1 0.2 d 2 0.4 d 3 0.5 d 4 NA d 5 NA Is there a way to do it without creating a dummy data frame with the first 2 columns then doing some sort of outer join with merge? Thanks. You can use the reshape2 package: #

Function to change Null to NA

僤鯓⒐⒋嵵緔 提交于 2019-11-28 13:31:16
I'm trying to write a function that turns Null values into NA. A summary of one of my column looks like this: a b 12 210 468 I'd like to change the 12 empty values to NA. I also have a few other factor columns for which I'd like to change Null values to NA, so I borrowed some stuff from here and there to come up with this: # change nulls to NAs nullToNA <- function(df){ # split df into numeric & non-numeric functions a<-df[,sapply(df, is.numeric), drop = FALSE] b<-df[,sapply(df, Negate(is.numeric)), drop = FALSE] # Change empty strings to NA b<-b[lapply(b,function(x) levels(x) <- c(levels(x),

Behavior of summing !is.na() results

北战南征 提交于 2019-11-28 10:54:11
Why does the first line return TRUE, and the third line returns 1? I would expect both lines to return 1. What is the exact meaning of those extra two parentheses in the third line? !is.na(5) + !is.na(NA) # TRUE (!is.na(5)) + (!is.na(NA)) # 1 edit: should check these multiple times. The original problem was with !is.na() , thought it replicated for is.na() . But it didn't :) ! has a weird, counter-intuitive precedence in R. Your first code is equivalent to !(is.na(5) + !is.na(NA)) That is, ! has lower precedence than + . 来源: https://stackoverflow.com/questions/17651687/behavior-of-summing-is

converting “1984-03-25 02:00:00” to POSIX gives NA

倾然丶 夕夏残阳落幕 提交于 2019-11-28 10:35:41
问题 While converting a vector of date-time to POSIXlt, just one particular time "25-Mar-1984-02:00" "is converted to POSIXlt but returns NA! So, this row was getting omitted in my analysis/plots. is.na(as.POSIXlt("25-Mar-1984-02:00",format = "%d-%b-%Y-%H:%M")) is.na(as.POSIXlt("25-Mar-1984-03:00",format = "%d-%b-%Y-%H:%M")) is.na(as.POSIXlt("25-Mar-1984-01:00",format = "%d-%b-%Y-%H:%M")) is.na(as.POSIXlt("24-Mar-1984-02:00",format = "%d-%b-%Y-%H:%M")) is.na(as.POSIXlt("26-Mar-1984-02:00",format =

fill in NA based on the last non-NA value for each group in R [duplicate]

蓝咒 提交于 2019-11-28 10:08:54
This question already has an answer here: Replace missing values (NA) with most recent non-NA by group 6 answers My question is I have a dataframe m as below y1 =c( rep("A",5),rep("B",5)) y2 = rep(c(1:5),2) y3 = y2 y3[c(2,7,9)]=NA m = data.frame(y1,y2,y3) y1 y2 y3 1 A 1 1 2 A 2 <NA> 3 A 3 3 4 A 4 4 5 A 5 5 6 B 1 1 7 B 2 <NA> 8 B 3 3 9 B 4 <NA> 10 B 5 5 I want to fill in the NA based on the closest non-NA value "in front of" this NA. My output should look like this: y1 y2 y3 y4 1 A 1 1 1 2 A 2 <NA> 1 3 A 3 3 3 4 A 4 4 4 5 A 5 5 5 6 B 1 1 1 7 B 2 <NA> 1 8 B 3 3 3 9 B 4 <NA> 3 10 B 5 5 5 Any idea

Using ifelse() to replace NAs in one data frame by referencing another data frame of different length

不问归期 提交于 2019-11-28 09:29:16
I already reviewed the following two posts and think they might answer my question, although I'm struggling to see how: 1) Conditional replacement of values in a data.frame 2) Creating a function to replace NAs from one data.frame with values from another With that said, I'm trying to replace NAs in one data frame by referencing another data frame of a different (shorter) length and pulling in replacement values from column "B" where the values for column "A" in each data frame match. I've modified the data, below, for simplicity and illustration, although the concept is the same in the actual

R: replacing NAs in a data.frame with values in the same position in another dataframe

橙三吉。 提交于 2019-11-28 09:02:23
I have a dataframe with some NA values: dfa <- data.frame(a=c(1,NA,3,4,5,NA),b=c(1,5,NA,NA,8,9),c=c(7,NA,NA,NA,2,NA)) dfa I would like to replace the NAs with values in the same position in another dataframe: dfrepair <- data.frame(a=c(2:7),b=c(6:1),c=c(8:3)) dfrepair I tried: dfa1 <- dfa dfa1 <- ifelse(dfa == NA, dfrepair, dfa) dfa1 but this did not work. You can do: dfa <- data.frame(a=c(1,NA,3,4,5,NA),b=c(1,5,NA,NA,8,9),c=c(7,NA,NA,NA,2,NA)) dfrepair <- data.frame(a=c(2:7),b=c(6:1),c=c(8:3)) dfa[is.na(dfa)] <- dfrepair[is.na(dfa)] dfa a b c 1 1 1 7 2 3 5 7 3 3 4 6 4 4 3 5 5 5 8 2 6 7 9 3 In

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

血红的双手。 提交于 2019-11-28 09:02:19
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 there's something I could do more globally/universally instead of doing it for each function. One

Hiding NA's when printing a dataframe in knitr

大憨熊 提交于 2019-11-28 08:57:25
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 make to the data frame or is it an xtable or knitr option I need to change? Sample knitr code:

Find names of columns which contain missing values

烂漫一生 提交于 2019-11-28 08:05:08
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 in 1:4){ tmp <- which(colnames(mymatrix[j, ]) %in% c("", "NA")) bad <- tmp } However, I keep getting