0
votes

I have the following data frame

>data.frame

col1    col2
          A
  x       B
          C
          D
  y       E

I need a new data frame that looks like:

>new.data.frame

  col1    col2
           A
   x       
           C
           D
   y       

I just need a method for reading from col1 and if there is ANY characters in Col1 then clear corresponding row value of col2. I was thinking about using an if statement and data.table for this but am unsure of how to relay the information for deleting col2's values based on ANY characters being present in col1.

3

3 Answers

2
votes

Something like this works:

# Create data frame
dat <- data.frame(col1=c(NA,"x", NA, NA, "y"), col2=c("A", "B", "C", "D", "E"))

# Create new data frame
dat_new <- dat
dat_new$col2[!is.na(dat_new$col1)] <- NA

# Check that it worked
dat
dat_new
1
votes

This depends on what you mean by 'remove'. Here I'm assuming a blank string "". However, the same principle will apply for NAs

## create data frame
df <- data.frame(col1 = c("", "x", "","", "y"),
                col2 = LETTERS[1:5], 
                stringsAsFactors = FALSE)

df
#   col1 col2
# 1         A
# 2    x    B
# 3         C
# 4         D
# 5    y    E

## subset by blank values in col1, and replace the values in col2
df[df$col1 != "",]$col2 <- ""
## or df$col2[df$col1 != ""] <- ""
df
#    col1 col2
# 1         A
# 2    x     
# 3         C
# 4         D
# 5    y     

And as you mentioned data.table, the code for this would be

library(data.table)

setDT(df)
## filter by blank entries in col1, and update col2 by-reference (:=)
df[col1 != "", col2 := ""]
df
1
votes

Using dplyr

library(dplyr)
df %>%
     mutate(col2 = replace(col2, col1!="", ""))
#     col1 col2
#1         A
#2    x     
#3         C
#4         D
#5    y