This question already has answers here:
How does subsetting with NA work?
(1 answer)
Why does x[NA] yield an NA vector the same length as x?
(1 answer)
Indexing integer vector with NA
(1 answer)
Closed 1 year ago.
c(0:5)[1] is 0.
c(0:5)[2] is 1.
But for the code
c(0:5)[NA]
The output is
[1] NA NA NA NA NA NA
Why is R showing 6 NAs as output?
The c in c(0:5) is redundant. 0:5 is already a vector. You would still need parentheses around 0:5 in (0:5)[NA] because of operator precedence.
The exact reason for what you see here depends on the implementation of [], but the result makes intuitive sense. [] isn't just used to extract single elements, it is used to extract subsets. If the index vector is unavailable, then for each element of the original vector, its status in the subset you are trying to extract is unavailable as well.
Related
This question already has answers here:
Replace NA in column with value in adjacent column
(3 answers)
Closed 1 year ago.
In the following case I want to replace the NA with the corresponding z column value. How do I assign it?
df<-data.frame(x=c(1,2,3,4,5,NA,7,NA,9,10),
z=c(1:10))
With this code I the NA is replaced with starting z value (1 and 2) and I need (6 and 8).
df$x[is.na(df$x)] <- (df$z)
We can use same the logic on the rhs
df$x[is.na(df$x)] <- df$z[is.na(df$x)]
This question already has answers here:
Remove columns from dataframe where some of values are NA
(8 answers)
Closed 3 years ago.
I have a 17(r) by 20 (c) matrix where all data is numbers and NA. I am trying to remove all columns that has the value NA in any rows. This is 11 of the 20 columns. I've been searching for an hour and tried several methods but couldn't get it right.
my.data [ ,!is.na(my.data[ ,1:20])]
To me this makes the most sense but is giving 'script too long' error.
One basic approach would be
mydata[, !is.na(colSums(mydata))]
This question already has answers here:
How to sum a numeric list elements
(2 answers)
Closed 6 years ago.
I have a list, each element in this list is a vector and have same length. I want to calculate the mean(or other value, it can be a user-defined function) of all first element of each vector, mean(or other value, it can be a user-defined function) of all second element of each vector, etc. And return a vector. So this is different from question How to sum a numeric list elements in R .Following code gave me the exactly what I want, however, is there any more efficient and sophistical way to do this? Thanks.
list1 <- list(a=1:5,b=2:6,c=3:7)
result <- numeric(length(list1[[1]]))
for(i in 1:length(list1[[1]])){
result[i] <- mean(c(list1[[1]][i],list1[[2]][i],list1[[3]][i])) #the function can be any other function rather than mean()
}
Here's an option using the Reduce function:
Reduce("+",list1)/length(list1)
[1] 2 3 4 5 6
How about putting them all in a matrix and then calculating the means of the columns?
colMeans(do.call(rbind, list1))
[1] 2 3 4 5 6
This question already has answers here:
Remove rows with all or some NAs (missing values) in data.frame
(18 answers)
Closed 7 years ago.
I have a dataframe:
a<-c(1,2,3)
b<-c("CarA","CarB",NA)
data<-data.frame(a,b)
data
a b
1 1 CarA
2 2 CarB
3 3 NA
Now I want to remove the row with missing data (NA).
But this does not work:
data<-data[data[,2]!=NA,]
My thinking here is to look at the second column [,2] and look for those that don't have NA. Then extract the remaining data. Would someone be able to tell me what went wrong here?
Wouldn't
na.omit(data)
do? It seems the cleanest and fastest way to me.
By the way, your code does not work because you cannot do !=NA
Use is.na() instead (but na.omit() is better):
data[!is.na(data[,2]),]
If you want some other answer check complete.cases
data[complete.cases(data),]
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Test for NA and select values based on result
Suppose you have a vector -- you do a calculation on the vector -- many of the elements return "NA" -- how do you identify these "NA"s and change them to some usable integer
Assuming that your data is in dat (could be a vector, matrix, or data frame):
dat[is.na(dat)]<-0
replaces all NA entries of dat with 0.