I'm working on a data set that looks as follow:
191
282 A
202
210 B
I would like to replace those empty cells at the second column with a character, say 'N'. How can I efficiently do this in R?
Appreciate it.
Alternatively, you can click the Home tab in the Ribbon and then select Go to Special from the Find & Select drop-down menu. Select Blanks in the Go To Special dialog box and click OK. Excel will select all of the blank cells within the range. Type the value you want to enter in the blanks (such as 0, – or text).
Use Excel's Find/Replace Function to Replace Zeros Choose Find/Replace (CTRL-H). Use 0 for Find what and leave the Replace with field blank (see below). Check “Match entire cell contents” or Excel will replace every zero, even the ones within values.
An example data frame:
dat <- read.table(text = "
191 ''
282 A
202 ''
210 B")
You can use sub
to replace the empty strings with "N"
:
dat$V2 <- sub("^$", "N", dat$V2)
# V1 V2
# 1 191 N
# 2 282 A
# 3 202 N
# 4 210 B
Another way:
Assuming the same data structure as wibeasley has put:
ds <- data.frame(ID=c(191, 282, 202, 210), Group=c("", "A", "", "B"), stringsAsFactors=FALSE)
you can just write:
ds$Group[ds$Group==""]<-"N"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With