0

(1) I have a data frame called COPY that looks like this

COPY <- data.frame (year  = c(values_here),
              Ceremony = c(values_here),
              Award = c(values_here),
              Winner = c(values_here),
              Name = c(values_here),
              Film = c(values_here),
              )

(2) Some of the entry in the name and film column for some rows are mixed up

(3) I created a vector of all the names in the wrong place using this code.

COPY$Film[COPY['Award']=='Director' & COPY['Year']>1930]->name

the entry's where the Award = director and the year is greater than 1930 the name and film columns are mixed

(4) Now I would like to replace COPY$Name based on the conditions stated with my new name object. I tried this code.

replace(COPY$Name,COPY$Award =='Director' && COPY$Year>1930,name)

SO basically I'm trying to flip the Name and Film columns where the Award column == director and the year column is greater than 1930.

CODERjack
  • 11
  • 2
  • Welcome to SO, CODERjack! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), and perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jul 29 '22 at 12:51

1 Answers1

0

Lacking data, try this:

COPY <- data.frame (year  = 2000:2002,
              Ceremony = NA,
              Award = c("A", "Director", "B"),
              Winner = NA,
              Name = c("A","B","C"),
              Film = c("1","2","3")
              )

swap <- COPY$Award == "Director"
COPY <- transform(COPY, Name = ifelse(swap, Film, Name), Film = ifelse(swap, Name, Film))
COPY
#   year Ceremony    Award Winner Name Film
# 1 2000       NA        A     NA    A    1
# 2 2001       NA Director     NA    2    B
# 3 2002       NA        B     NA    C    3
r2evans
  • 141,215
  • 6
  • 77
  • 149