2

I am trying to replace a certain 4/5 letter code with a longer name in a dataframe created by the ReShaped melt() function. However, its not working.

What I have at the start (shortened version):

       Index Fund       Value
1 2021.06.01 VDEE  0.00000000
2 2021.06.02 VDEE -0.04419429
3 2021.06.03 VDEE -0.24161782
4 2021.06.04 VDEE -0.03707800
5 2021.06.07 VDEE  0.56371383

What I want to end up with:

       Index                                   Fund       Value
1 2021.06.01 FTSEDevelopedEuropeexUKEquityIndexFund  0.00000000
2 2021.06.02 FTSEDevelopedEuropeexUKEquityIndexFund -0.04419429
3 2021.06.03 FTSEDevelopedEuropeexUKEquityIndexFund -0.24161782
4 2021.06.04 FTSEDevelopedEuropeexUKEquityIndexFund -0.03707800
5 2021.06.07 FTSEDevelopedEuropeexUKEquityIndexFund  0.56371383

What I currently end up with:

       Index Fund       Value
1 2021.06.01 <NA>  0.00000000
2 2021.06.02 <NA> -0.04419429
3 2021.06.03 <NA> -0.24161782
4 2021.06.04 <NA> -0.03707800
5 2021.06.07 <NA>  0.56371383

Basically I want to replace a short code with a longer string in the dataframe. However the main way to do this I have found is: ReShaped[ReShaped == Fund] <- Name. But when I do this I get the following error: invalid factor level, NA generated. Which led me to this. However when I add the stringsAsFactors = FALSE argument to the melt() function I still get the error, and the same if I do this ReShaped[ReShaped == Fund] <- factor(Name)

Any Ideas on what the problem is or how to solve it?

Note: I would like to keep the same format of the dataframe as I am then going to plot it in ggplot2.

  • Just to clarify - my question is different to the other linked questions because firstly, I am creating it through the ReShaped module and secondly, if it was the same question then surely one of the top 3 answers would work? – James Ashwood Jul 13 '22 at 07:40
  • I have ended doing the columns in a different way however it still poses the question why this was not working. – James Ashwood Jul 13 '22 at 08:55

1 Answers1

0

We may create a key/val dataset and do a join

library(dplyr)
 keydat <- tibble(Fund = c("VDEE"), val = c("FTSEDevelopedEuropeexUKEquityIndexFund"))
df1 <- df1 %>% 
   left_join(keydat) %>%
    mutate(Fund = coalesce(val, Fund), .keep = "unused")

-output

df1
  Index                                   Fund       Value
1 2021.06.01 FTSEDevelopedEuropeexUKEquityIndexFund  0.00000000
2 2021.06.02 FTSEDevelopedEuropeexUKEquityIndexFund -0.04419429
3 2021.06.03 FTSEDevelopedEuropeexUKEquityIndexFund -0.24161782
4 2021.06.04 FTSEDevelopedEuropeexUKEquityIndexFund -0.03707800
5 2021.06.07 FTSEDevelopedEuropeexUKEquityIndexFund  0.56371383

data

df1 <- structure(list(Index = c("2021.06.01", "2021.06.02", "2021.06.03", 
"2021.06.04", "2021.06.07"), Fund = c("VDEE", "VDEE", "VDEE", 
"VDEE", "VDEE"), Value = c(0, -0.04419429, -0.24161782, -0.037078, 
0.56371383)), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5"))
akrun
  • 874,273
  • 37
  • 540
  • 662