0

I am having difficulty renaming values in a data.frame using plyr. This appears to be a somewhat common problem, but other, similar Q/As aren't working for me. I am using plyr version: 1.8.4

As noted in this 108 point thread, the following code should work...

I must be making some simple error, but I don't know where.

Code:

library(plyr)    
df <-plyr::rename(df, c("mod1"="AT", "mod2"="CYP", "mod3"="OA"))

resulting error: The following from values were not present in x: mod1, mod2, mod3

Other Q/As have said to disregard this error, or to use setnames() from the data.table package. setnames() results in an error, as does dplyr's rename function. The issue is also definitely not due to loading both dplyr and plyr.

This solution from the another thread also doesn't appear to change anything:

names(df)[names(df) == 'mod1'] <- 'AT'

data:

df <- structure(list(dose = c(0.5, 0.565608284362011, 0.639825462677876,0.723781164472726, 0.818753245381915, 0.5, 0.565608284362011,0.639825462677876, 0.723781164472726, 0.818753245381915, 0.926187236872587,0.5, 0.565608284362011, 0.639825462677876, 0.723781164472726,0.818753245381915, 0.926187236872587), p1 = c(0.0103075076812739,0.0116952370538794, 0.0132672958208565, 0.0150474513444454, 0.017062331184752,0.0103075076812739, 0.0116952370538794, 0.0132672958208565, 0.0150474513444454,0.017062331184752, 0.0193417088954045, 0.0103075076812739, 0.0116952370538794,0.0132672958208565, 0.0150474513444454, 0.017062331184752, 0.0193417088954045), pmin1 = c(0.00536333319279742, 0.00625496575175793, 0.00728840578671532,0.00848520965537471, 0.00987000073136779, 0.00536333319279742,0.00625496575175793, 0.00728840578671532, 0.00848520965537471,0.00987000073136779, 0.0114708496853662, 0.00536333319279742,0.00625496575175793, 0.00728840578671532, 0.00848520965537471,0.00987000073136779, 0.0114708496853662), pmax1 = c(0.0152516821697505,0.0171355083560008, 0.0192461858549976, 0.0216096930335161, 0.0242546616381362,0.0152516821697505, 0.0171355083560008, 0.0192461858549976, 0.0216096930335161,0.0242546616381362, 0.0272125681054427, 0.0152516821697505, 0.0171355083560008,0.0192461858549976, 0.0216096930335161, 0.0242546616381362, 0.0272125681054427), model = c("mod1", "mod1", "mod1", "mod1", "mod1", "mod2","mod2", "mod2", "mod2", "mod2", "mod2", "mod3", "mod3", "mod3","mod3", "mod3", "mod3")), .Names = c("dose", "p1", "pmin1", "pmax1","model"), row.names = c(1L, 2L, 3L, 4L, 5L, 101L, 102L, 103L,104L, 105L, 106L, 201L, 202L, 203L, 204L, 205L, 206L), class = "data.frame")

This is incredibly strange for me, because I believe this code was previously working for me!

Arch
  • 192
  • 2
  • 16
  • 2
    Look at your data; they're not names, they're values in the `model` variable. – alistaire Nov 22 '16 at 00:22
  • I understand - but how can I change all of the strings named "mod1" in the model column to something else? I am nearly certain this code worked a week ago. – Arch Nov 22 '16 at 02:01
  • There are lots of options, but you're already sort of built for a lookup table: `df$new_variable <- c("mod1"="AT", "mod2"="CYP", "mod3"="OA")[df$model]` If it worked before, your data must have been in a wider format. – alistaire Nov 22 '16 at 03:14
  • Another option: `dplyr::recode(df$model, "mod1"="AT", "mod2"="CYP", "mod3"="OA")` – pe-perry Nov 22 '16 at 03:28
  • @ alistaire, this solution works great ~ if you were to post it as an answer I would accept it. @ kitman0804 , I've tried your solution as well, but it appears to not change the original file, and when applying the solution to my true data, received some errors that recode cannot be applied to a data.frame – Arch Nov 22 '16 at 12:12

0 Answers0