na

R - split data frame without removing NA values

纵然是瞬间 提交于 2019-12-11 06:05:57
问题 If I have a df: letter body_part a head b head c NA d NA e left_foot And I want to split it into 2 dfs... One with only body_part - "head" and the other with everything else. I.e. list <- split(df, df$body_part == 'head') Can I do that without dropping the NA rows? (I know I can do it if I fill the NAs with a string, but is there a way that avoids that step?) 回答1: From ?`%in%` : That ‘%in%’ never returns ‘NA’ makes it particularly useful in ‘if’ conditions. # just to show how the `==`

positions of non-NA cells in a matrix

≯℡__Kan透↙ 提交于 2019-12-11 04:47:45
问题 Consider the following matrix, m <- matrix(letters[c(1,2,NA,3,NA,4,5,6,7,8)], 2, byrow=TRUE) ## [,1] [,2] [,3] [,4] [,5] ## [1,] "a" "b" NA "c" NA ## [2,] "d" "e" "f" "g" "h" I wish to obtain the column indices corresponding to all non-NA elements, merged with the NA elements immediately following: result <- c(list(1), list(2:3), list(4,5), list(1), list(2), list(3), list(4), list(5)) Any ideas? 回答1: The column (and row) indicies of non-NA elements can be obtained with which(!is.na(m), TRUE)

Replace Inf in R data.table / Show number of Inf in colums

点点圈 提交于 2019-12-11 04:09:06
问题 I can't figure out how to use an is.na(x) like function for infinite numbers in R with a data table or show per column how many Inf's there are: colSums(is.infinite(x)) I use the following example data set: DT <- data.table(a=c(1/0,1,2/0),b=c("a","b","c"),c=c(1/0,5,NA)) DT a b c 1: Inf a Inf 2: 1 b 5 3: Inf c NA colSums(is.na(DT)) a b c 0 0 1 colSums(is.infinite(DT)) Error in is.infinite(DT) : default method not implemented for type 'list' DT[is.na(DT)] <- 100 DT a b c 1: Inf a Inf 2: 1 b 5 3

Remove NAs from data frame without deleting entire rows/columns

拟墨画扇 提交于 2019-12-11 03:28:36
问题 I'm analyzing some pilot data for an experiment where we are giving participants 60 pairs of auditory stimuli from a pool of 190 pairs to rate on a 4 point scale. I get a lot of missing values since the participants are rating different pairs each time. I really don't care about which participant said what, I just need all the ratings for the same pair to be in the same row so I can perform a Light's Kappa test for inter-rater agreement on each pair in n with kappam.light (irr package). Here

R replacing columns by lookup to dictionary

。_饼干妹妹 提交于 2019-12-11 02:33:38
问题 In this question I need to be able to lookup a value from a dataframe's column not only based on one attribute, but based on more attributes and range comparing against a dictionary. (Yes, this is actually a continuation of a story in R conditional replace more columns by lookup ) It should be easy question for R-known ppl, because I provide working solution for basic indexing, that needs to be upgraded, possibly easily ... but it is very hard for me, because Iam in a process of learning R.

Aggregate raster in R with NA values

≡放荡痞女 提交于 2019-12-11 02:25:22
问题 I have a 1km resolution raster in R with widespread NA values throughout, but at irregular locations (i.e. the cells with data are not contiguous and have NA values scattered throughout). I am trying to aggregate this raster (using aggregate() command in the {raster} package) at, say, 5km resolution (factor=5) with a user-defined function for averaging circular angles (included below). As of now, I can't figure how to get aggregate() (or my function, if that's the problem) to provide a result

removing NA values from a DataFrame in Python 3.4

二次信任 提交于 2019-12-11 02:06:53
问题 import pandas as pd import statistics df=print(pd.read_csv('001.csv',keep_default_na=False, na_values=[""])) print(df) I am using this code to create a data frame which has no NA values. I have couple of CSV files and I want to calculate Mean of one of the columns - sulfate. This column has many 'NA' values, which I am trying to exclude. Even after using the above code, 'NA's aren't excluded from the data frame. Please suggest. 回答1: I think you should import the .csv file as it is and then

Conditional replacement of NAs in two dataframes R

自古美人都是妖i 提交于 2019-12-11 01:46:32
问题 Probably simple but tricky question especially for larger data sets. Given two dataframes ( df1 , df2 ) of equal dimensions as below: head(df1) a b c 1 0.8569720 0.45839112 NA 2 0.7789126 0.36591578 NA 3 0.6901663 0.88095485 NA 4 0.7705756 0.54775807 NA 5 0.1743111 0.89087819 NA 6 0.5812786 0.04361905 NA and head(df2) a b c 1 0.21210312 0.7670091 NA 2 0.19767464 0.3050934 1 3 0.08982958 0.4453491 2 4 0.75196925 0.6745908 3 5 0.73216793 0.6418483 4 6 0.73640209 0.7448011 5 How can one find all

How to simply count number of rows with NAs - R [duplicate]

末鹿安然 提交于 2019-12-11 01:07:43
问题 This question already has answers here : Determine the number of rows with NAs (5 answers) Closed last year . I'm trying to compute the number of rows with NA of the whole df as I'm looking to compute the % of rows with NA over the total number of rows of the df. I have already have seen this post: Determine the number of rows with NAs but it just shows a specific range of columns. 回答1: tl;dr: row wise, you'll want sum(!complete.cases(DF)) , or, equivalently, sum(apply(DF, 1, anyNA)) There

How to select the last one test without NA in r

落爺英雄遲暮 提交于 2019-12-10 23:40:01
问题 My dataframe is similar like this: Person W.1 W.2 W.3 W.4 W.5 1 62 57 52 59 NA 2 49 38 60 NA NA 3 59 34 NA NA NA Is there a way to select the first and last test without "NA". I have 300 data entries, and W.1 means the first test, W.2 means the second test, W.n means the nth test. I want to compare the score of the first test with the score of the last test. For example, I want to compare: 1 62 59 2 49 60 3 59 34 But different persons have different places having "NA", can someone help me?