na

Collapsing rows where some are all NA, others are disjoint with some NAs

吃可爱长大的小学妹 提交于 2019-11-26 11:25:36
I have a simple dataframe as such: ID Col1 Col2 Col3 Col4 1 NA NA NA NA 1 5 10 NA NA 1 NA NA 15 20 2 NA NA NA NA 2 25 30 NA NA 2 NA NA 35 40 And I would like to reformat it as such: ID Col1 Col2 Col3 Col4 1 5 10 15 20 2 25 30 35 40 (please note: the real data set has thousands of rows and the values are from biological data -- the NA s follow no simple pattern, except that the NA s are disjoint, and yes there are exactly 3 rows for each ID ). STEP ONE : get rid of rows that have only NA values. On the surface this looked simple, but I've run across some problems. complete.cases(DF) returns all

How to avoid warning when introducing NAs by coercion

筅森魡賤 提交于 2019-11-26 09:16:54
问题 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? 回答1: Use suppressWarnings(

Dealing with TRUE, FALSE, NA and NaN

泪湿孤枕 提交于 2019-11-26 07:35:12
问题 Here is a vector a <- c(TRUE, FALSE, FALSE, NA, FALSE, TRUE, NA, FALSE, TRUE) I\'d like a simple function that returns TRUE everytime there is a TRUE in \"a\", and FALSE everytime there is a FALSE or a NA in \"a\". The three following things do not work a == TRUE identical(TRUE, a) isTRUE(a) Here is a solution a[-which(is.na(a))] but it doesn\'t seem to be a straightforward and easy solution Is there another solution ? Here are some functions (and operators) I know: identical() isTRUE() is.na

How to replace NA values in a table for selected columns

天大地大妈咪最大 提交于 2019-11-26 05:55:42
问题 There are a lot of posts about replacing NA values. I am aware that one could replace NAs in the following table/frame with the following: x[is.na(x)]<-0 But, what if I want to restrict it to only certain columns? Let\'s me show you an example. First, let\'s start with a dataset. set.seed(1234) x <- data.frame(a=sample(c(1,2,NA), 10, replace=T), b=sample(c(1,2,NA), 10, replace=T), c=sample(c(1:5,NA), 10, replace=T)) Which gives: a b c 1 1 NA 2 2 2 2 2 3 2 1 1 4 2 NA 1 5 NA 1 2 6 2 NA 5 7 1 1

Change the Blank Cells to “NA”

烈酒焚心 提交于 2019-11-26 03:49:31
问题 Here\'s the link of my data. My target is to assign \"NA\" to all blank cells irrespective of categorical or numerical values. I am using na.strings=\"\" . But it\'s not assigning NA to all blank cells. ## reading the data dat <- read.csv(\"data2.csv\") head(dat) mon hr acc alc sex spd axles door reg cond1 drug1 1 8 21 No Control TRUE F 0 2 2 Physical Impairment (Eyes, Ear, Limb) A 2 7 20 No Control FALSE M 900 2 2 Inattentive D 3 3 9 No Control FALSE F 100 2 2 2004 Normal D 4 1 15 No Control

How to delete rows from a dataframe that contain n*NA

感情迁移 提交于 2019-11-26 03:45:42
问题 I have a number of large datasets with ~10 columns, and ~200000 rows. Not all columns contain values for each row, although at least one column must contain a value for the row to be present, I would like to set a threshold for how many NA s are allowed in a row. My Dataframe looks something like this: ID q r s t u v w x y z A 1 5 NA 3 8 9 NA 8 6 4 B 5 NA 4 6 1 9 7 4 9 3 C NA 9 4 NA 4 8 4 NA 5 NA D 2 2 6 8 4 NA 3 7 1 32 And I would like to be able to delete the rows that contain more than 2

suppress NAs in paste()

一个人想着一个人 提交于 2019-11-26 02:57:50
问题 Regarding the bounty Ben Bolker\'s paste2 -solution produces a \"\" when the strings that are pasted contains NA \'s in the same position. Like this, > paste2(c(\"a\",\"b\", \"c\", NA), c(\"A\",\"B\", NA, NA)) [1] \"a, A\" \"b, B\" \"c\" \"\" The fourth element is an \"\" instead of an NA Like this, [1] \"a, A\" \"b, B\" \"c\" NA I\'m offering up this small bounty for anyone who can fix this. Original question I\'ve read the help page ?paste , but I don\'t understand how to have R ignore NA s

Collapsing rows where some are all NA, others are disjoint with some NAs

倾然丶 夕夏残阳落幕 提交于 2019-11-26 02:38:46
问题 I have a simple dataframe as such: ID Col1 Col2 Col3 Col4 1 NA NA NA NA 1 5 10 NA NA 1 NA NA 15 20 2 NA NA NA NA 2 25 30 NA NA 2 NA NA 35 40 And I would like to reformat it as such: ID Col1 Col2 Col3 Col4 1 5 10 15 20 2 25 30 35 40 (please note: the real data set has thousands of rows and the values are from biological data -- the NA s follow no simple pattern, except that the NA s are disjoint, and yes there are exactly 3 rows for each ID ). STEP ONE : get rid of rows that have only NA

How to replace NA with mean by group / subset?

浪子不回头ぞ 提交于 2019-11-26 02:35:51
问题 I have a dataframe with the lengths and widths of various arthropods from the guts of salamanders. Because some guts had thousands of certain prey items, I only measured a subset of each prey type. I now want to replace each unmeasured individual with the mean length and width for that prey. I want to keep the dataframe and just add imputed columns (length2, width2). The main reason is that each row also has columns with data on the date and location the salamander was collected. I could fill

Replace NA in column with value in adjacent column

无人久伴 提交于 2019-11-26 02:21:32
问题 This question is related to a post with a similar title (replace NA in an R vector with adjacent values). I would like to scan a column in a data frame and replace NA\'s with the value in the adjacent cell. In the aforementioned post, the solution was to replace the NA not with the value from the adjacent vector (e.g. the adjacent element in the data matrix) but was a conditional replace for a fixed value. Below is a reproducible example of my problem: UNIT <- c(NA,NA, 200, 200, 200, 200, 200