Is there a way to force a change in variable values in R?
temp <- replace(mydata4$Age,is.na(mydata4$Age),28)
mydata4$Age <- temp
mydata4$Age
Change is made but when I do following code, NA's are back
names(mydata4)
head(mydata4$Age,20)
#save(mydata4,file="mydata4.Rda")
#df <- load(file="mydata4.Rda")
#df <- as.data.frame(df)
df <- as.data.frame(mydata4)
names(df)
head(df,20)
df$Age
df
Age contains NA's
How to fix?
FJCC
2
I cannot reproduce your result. Does the following code work for you? What is the structure of mydata4 at the beginning of the code?
mydata4 <- data.frame(Age = c(32,61,NA,7), Value =1:4)
mydata4
Age Value
1 32 1
2 61 2
3 NA 3
4 7 4
temp <- replace(mydata4$Age,is.na(mydata4$Age),28)
mydata4$Age <- temp
mydata4$Age
[1] 32 61 28 7
names(mydata4)
[1] "Age" "Value"
head(mydata4$Age,20)
[1] 32 61 28 7
df <- as.data.frame(mydata4)
names(df)
[1] "Age" "Value"
head(df,20)
Age Value
1 32 1
2 61 2
3 28 3
4 7 4
df$Age
[1] 32 61 28 7
df
Age Value
1 32 1
2 61 2
3 28 3
4 7 4
It turns out that the as.data.frame part of the code is critical that is
df <- as.data.frame(mydata4)
Thanks.
M
system
Closed
4
This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.