na

How to remove NA values in vector in R [duplicate]

给你一囗甜甜゛ 提交于 2019-11-30 17:58:41
This question already has an answer here: Remove NA values from a vector 6 answers I have a vector which stores over 1000 values. The first 50 values are NAs, how can I get rid of it? c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1.5741, 1.583, 1.605, 1.633, 1.6465, 1.6475, 1.6329, 1.6413, 1.685, 1.692, 1.7087, 1.7055, 1.6985, 1.6807, 1.6745, 1.673, 1.6625, 1.6805, 1.689, 1.667, 1.684, 1.6675, 1.6867, 1.6688, 1.6643, 1.6685, 1.7025, 1.737,

Multiple na.strings in read.table() function in R

一曲冷凌霜 提交于 2019-11-30 17:34:01
问题 I have a square table and it has two na.strings (e.g. "A" and "B") that I need to turn into NA. So far I can turn either one of those into NA but not both. How should I do this? Can I use a function in that argument? If yes, what function should I use? I tried like ( na.strings = "A" | "B" ) and ( na.strings = "A | B" ) but it does not work. My code is as follows: loadfile<-read.table("test.csv", header=T, sep=",", na.strings="A | B") 回答1: na.strings takes a character vector, so... loadfile <

Pandas: Why is default column type for numeric float?

时光毁灭记忆、已成空白 提交于 2019-11-30 15:30:00
问题 I am using Pandas 0.18.1 with python 2.7.x. I have an empty dataframe that I read first. I see that the types of these columns are object which is OK. When I assign one row of data, the type for numeric values changes to float64 . I was expecting int or int64 . Why does this happen? Is there a way to set some global option to let Pandas knows that for numeric values, treat them by default as int unless the data has a . ? For example, [0 1.0, 2.] , first column is int but other two are float64

Filling dict with NA values to allow conversion to pandas dataframe

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 14:45:03
问题 I have a dict that holds computed values on different time lags, which means they start on different dates. For instance, the data I have may look like the following: Date col1 col2 col3 col4 col5 01-01-15 5 12 1 -15 10 01-02-15 7 0 9 11 7 01-03-15 6 1 2 18 01-04-15 9 8 10 01-05-15 -4 7 01-06-15 -11 -1 01-07-15 6 Where each header is the key, and each column of values is the value for each key (I'm using a defaultdict(list) for this). When I try to run pd.DataFrame.from_dict(d) I

Filling dict with NA values to allow conversion to pandas dataframe

最后都变了- 提交于 2019-11-30 11:51:50
I have a dict that holds computed values on different time lags, which means they start on different dates. For instance, the data I have may look like the following: Date col1 col2 col3 col4 col5 01-01-15 5 12 1 -15 10 01-02-15 7 0 9 11 7 01-03-15 6 1 2 18 01-04-15 9 8 10 01-05-15 -4 7 01-06-15 -11 -1 01-07-15 6 Where each header is the key, and each column of values is the value for each key (I'm using a defaultdict(list) for this). When I try to run pd.DataFrame.from_dict(d) I understandably get an error stating that all arrays must be the same length. Is there an easy/trivial way to fill

R is there a way to find Inf/-Inf values?

拜拜、爱过 提交于 2019-11-30 11:50:00
I'm trying to run a randomForest on a large-ish data set (5000x300). Unfortunately I'm getting an error message as follows: > RF <- randomForest(prePrior1, postPrior1[,6] + ,,do.trace=TRUE,importance=TRUE,ntree=100,,forest=TRUE) Error in randomForest.default(prePrior1, postPrior1[, 6], , do.trace = TRUE, : NA/NaN/Inf in foreign function call (arg 1) So I try to find any NA's using : > df2 <- prePrior1[is.na(prePrior1)] > df2 character(0) > df2 <- postPrior1[is.na(postPrior1[,6])] > df2 numeric(0) which leads me to believe that it's Inf's that are the problem as there don't seem to be any NA's.

Creating a function to replace NAs from one data.frame with values from another

有些话、适合烂在心里 提交于 2019-11-30 06:38:34
I regularly have situations where I need to replace missing values from a data.frame with values from some other data.frame that is at a different level of aggregation. So, for example, if I have a data.frame full of county data I might replace NA values with state values stored in another data.frame. After writing the same merge ... ifelse(is.na()) yada yada a few dozen times I decided to break down and write a function to do this. Here's what I cooked up, along with an example of how I use it: fillNaDf <- function(naDf, fillDf, mergeCols, fillCols){ mergedDf <- merge(naDf, fillDf, by

R: Adding NAs into Data Frame

一笑奈何 提交于 2019-11-30 06:01:14
问题 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

Aggregate with na.action=na.pass gives unexpected answer

主宰稳场 提交于 2019-11-30 05:42:28
问题 I use the following data.frame as an example: d <- data.frame(x=c(1,NA), y=c(2,3)) I'd like to sum up the values of y by the variable x. Since there is no common value of x, I would expect aggregation to just give me the original data.frame back, where NA is treated as a group. But aggregation gives me the following results. >aggregate(y ~ x, data=d, FUN=sum) x y 1 1 2 I've read the documentation about changing the default actions of na.action, but it doesn't seem to give me anything

How to Replace Raster Values Less than 0 to NA in R code

送分小仙女□ 提交于 2019-11-30 03:06:28
问题 I am working with Landsat imagery to calculate net radiation. There are very low negative values (ex: -0.000003) that are in my converted reflectance raster layers. I want to make sure that my reflectances are 0 - 1 in order to reduce error in my future calculations. How can I replace raster values less than 0 to "NA" in R. Similar to a raster calc function. I'm unsure how to provide an example, but I'm certain one of you could help me, yes? Here is my equation for the reflectivity derived