R recode values in dataframe with condition [duplicate] - r

This question already has answers here:
Dictionary style replace multiple items
(11 answers)
Multiple replacement in R
(3 answers)
How to replace multiple values at once [duplicate]
(2 answers)
Closed 3 months ago.
i have a dataframe
namer None namei ... None
None None Na ... namej
.
.
named namev None ... Na
and i want to replace all Nones with 0 and everything else with 1
i tried to use lapply with the following function
fu = function(x){
if (x=="None")
0
else
1}
but it throws an error, how can i fix this

Related

replace the NA value [duplicate]

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

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

R replacing NaN with 0 using dplyr [duplicate]

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

How do I rename the column 0 in a dataframe? [duplicate]

This question already has answers here:
How do I name the "row names" column in r
(2 answers)
Understanding array indexing in R
(2 answers)
Closed 3 years ago.
I have a dataframe with 4 columns. How do I give the first column a name?
I tried using dplyr but it doesn't work.
colnames(df)[0] <- "Test"
That's not a column, those are the row names. Within the tidyverse there's a function to create a column out of them:
df<-tibble::rownames_to_column(df, "test")

R: how to use if myValue.is.na? [duplicate]

This question already has answers here:
Set NA to 0 in R
(6 answers)
How do I replace NA values with zeros in an R dataframe?
(29 answers)
Closed 5 years ago.
Basically, I am iterating through some values, some which may be NA. If it is NA, I'd like to set it to 0.
For example
if (myValue.is.na){
myValue=0
}

Resources