How to replace values within dataframe

Hi there,

I was trying to know how to replace NA values in df1new$Weight.R by df1new$Weight.

Please if someone know about it, let me know.

Thank you.

Assuming your NA values are actually "None" as in your example, you can use the ifelse function from the base package - base::ifelse

df1new$Weight.R <- ifelse(df1new$Weight.R == "None", df1new$Weight, df1new$Weight.R)

hi Michael, thank you for your answer

I've applied your code but:

df1new$Weight.R <- ifelsE(df1new$Weight.R = NA, df1new$Weight, df1new$Weight.R)

Error: unexpected '=' in "df1new$Weight.R <- ifelsE(df1new$Weight.R ="

I am trying with NA instead of None

Ok so if they are truly NA, you instead use as the check condition inside of ifelse. Try:

df1new$Weight.R <- ifelse($Weight.R), df1new$Weight, df1new$Weight.R)


Michael Garcia answered you well, but as a learning point, to test for equality use == I.e. double =

A single = means assignment the same as <-

1 Like

Yupi!!!! Thank you very much Michael, I was able to run the code successfully. :slight_smile:
P.s. Sorry about the delay because I've lost the code and I had to do it again.
Thank you!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.