replace the NA value [duplicate] - r

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)]

Related

Explain output for c(0:5)[NA] [duplicate]

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.

is there a way to set all cells in a dataframe in the form of a vector as NA? [duplicate]

This question already has answers here:
R: Count number of objects in list [closed]
(5 answers)
Closed 2 years ago.
I have a dataframe in R, and I am trying to set all cells in the form of a vector, either c(1,2,3) or 1:2 to NA. Is there any easy way to do this?
You can use lengths to count number of elements in each value of column. Set them to NA where the length is greater than 1. Here I am considering dataframe name as df and column name as col_name. Change them according to your data.
df$col_name[lengths(df$col_name) > 1] <- NA

Delete vector elements in a vector [duplicate]

This question already has answers here:
How to tell what is in one vector and not another?
(6 answers)
Closed 5 years ago.
I try to do this in R:(for example)
let x = c(1,2,3,4,5,6,7,8) and y=c(1,2,8)
So
x[x!=y] = numeric(0) ????
I want to get as a result 3,4,5,6,7
Is there a practical way to do this?
Thanks
Use value matching %in% and remove the elements of x that are present in y
x[-which(x %in% y)]
#[1] 3 4 5 6 7

How to calculate NA's in each column and drop columns having more than 100 NA's [duplicate]

This question already has answers here:
R: how to total the number of NA in each col of data.frame
(9 answers)
Closed 6 years ago.
Dataset attribute headings
I am a beginner and I am trying something like this:
for (i in newTrain) {
count = 0
count = length(which(is.na(newTrain$i)))
names(-which(count>100))
}
but this isn't working at all for me.
We could first apply is.na for the entire dataframe and then sum the value of NAs for every column. Then select columns which have NA value less than 100.
newTrain[colSums(is.na(newTrain)) < 100]

R programming ifelse() programming [duplicate]

This question already has answers here:
Replace missing values with column mean
(14 answers)
Closed 6 years ago.
I have a dataset named malt, where one of the columns is named ka. I want to replace NA values in that ka column by mean values in malt$ka and other value remain as it is, so do this by if else
malt$ka <- ifelse(malt$ka=="NA", mean(malt$ka), "malt$AcqCostPercust")
This does not seem to work, and I am confused how to replace values the NA values.
Or
malt$ka[is.na(malt$ka)] <- mean(malt$ka, na.rm = TRUE)
x <- mean(malt$ka, na.rm=T) # assign mean value to a variable x
malt$ka<-ifelse(is.na(malt$ka),x,malt$ka)

Resources