R replacing NaN with 0 using dplyr [duplicate] - r

This question already has answers here:
Replace NA with Zero in dplyr without using list()
(5 answers)
Replace NA in all columns of a dplyr chain
(1 answer)
How to replace NaN value with zero in a huge data frame?
(4 answers)
R - convert nan to 0 results in all 0's
(1 answer)
Closed 3 years ago.
My dataframe looks like this -
dataset = data.frame(ID=c(1:3),Count=c(22,NaN,13))
I'm trying to use the pipe operator to replace NaN with 0
dataset = dataset %>% replace('NaN',0)
However this doesn't work.
I've looked at the solutions on this websites, but none seem to work.
Any inputs would be highly appreciated.

This'll do it, and doesn't even require dplyr as it's in base:
dataset$Count[is.nan(dataset$Count)]<-0

Related

My dataset as "?" values. How can I find all the missing values with R? [duplicate]

This question already has answers here:
Replacing character values with NA in a data frame
(7 answers)
Closed 2 years ago.
I'm new to R and I'm trying to find all the missing values in my data set. I'm using R and I tried the is.na function and it didn't work. Missing entries in my dataset have a "?"
df[df=="?"] <- NA

How to find the location of missing values in the data set in R? [duplicate]

This question already has answers here:
Finding the index of an NA value in a vector [duplicate]
(1 answer)
How can I find the index of all NA in a dataframe column?
(1 answer)
Closed 2 years ago.
Is there any function to indicate the location of missing values in R?
If df is the name of your dataframe them which(is.na(df))
Yes - use the is.na() function. There are some examples in the R documentation, but basically it returns a boolean value.

!is.na Remove columns with NA value (in any rows) [duplicate]

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

How to get a list of all the different values within a variable [duplicate]

This question already has answers here:
Extracting unique rows from a data table in R [duplicate]
(2 answers)
Closed 4 years ago.
I've always wondered if there's a command in R that gives you the entire domain of values within a variable.
For example, let's say I have the following data.table:
dt
Household Number_of_children
1 0
2 3
3 3
Is there a command along the lines of summary() or str() that would return the list 0, 3?
I believe summary and str only do that when your variable is a character string. I don't know how to do this when your variable is an integer, numeric, etc.
You can use unique() function, with column/vector as input.
unique(dt[,'Number_of_children'])

Need to convert a column or data to 0 or 1 in R [duplicate]

This question already has answers here:
Generate a dummy-variable
(17 answers)
Closed 6 years ago.
I am doing an assignment with the built-in "SWISS" dataset, and where the value is greater than 50 I need to create a binary variable taking 1
I have a feeling that I might have to use the subset command, but I am lost as how to implement it.
using dplyr
swiss %>%
mutate(aggGreaterThan50 = (Agriculture > 50) * 1)

Resources