Replace Numbers in Data Frame Column in R

Replacing numbers to letters in dataframe in r

No loop required:

df <- data.frame(well = c("0","1",NA,"1","1","2","2","3","4","3"))

lxn <- data.frame(old_name = c(0,1,2,3,4,5,6,7),
new_name = c("A","B","C","D","E","F","G","H"))

df %>%
mutate(well = set_names(lxn$new_name, lxn$old_name)[well])

# well
# 1 A
# 2 B
# 3 <NA>
# 4 B
# 5 B
# 6 C
# 7 C
# 8 D
# 9 E
# 10 D

Replace numbers with strings in multiple columns, R

Using dplyr:

Code:

df %>% 
mutate(across(1:3,
~ case_when(
. == 1 ~ "cat",
. == 2 ~ "dog",
. == 3 ~ "rabbit",
TRUE ~ NA_character_
)))

Output:

       A1     A2     A3    A4    A5
<char> <char> <char> <int> <int>
1: cat rabbit <NA> 5 2
2: cat dog cat 2 1
3: rabbit <NA> <NA> 1 8
4: dog cat <NA> 1 12

Data:

df <- structure(list(A1 = c(1L, 1L, 3L, 2L), A2 = c(3L, 2L, NA, 1L), 
A3 = c(NA, 1L, NA, NA), A4 = c(5L, 2L, 1L, 1L), A5 = c(2L,
1L, 8L, 12L)), row.names = c(NA, -4L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x00000222c4081ef0>)

replace column value from number to text

Not the most elegant or programmatic solution, but you could replace all the values with the mutate() function from the dplyr package:

library(dplyr)

df = mutate(df, Month = case_when(Month == "1" ~ "Jan",
Month == "2" ~ "Feb",
Month == "3" ~ "Mar",
Month == "4" ~ "Apr",
Month == "5" ~ "May",
Month == "6" ~ "Jun",
Month == "7" ~ "Jul",
Month == "8" ~ "Aug",
Month == "9" ~ "Sep",
Month == "10" ~ "Oct",
Month == "11" ~ "Nov",
Month == "12" ~ "Dec")

You can find documentation for using case_when() here: https://dplyr.tidyverse.org/reference/case_when.html

An alternative programmatic solution based on the comment left by @r2evans to do this in one line using the built-in R object month.abb:

df = mutate(df, Month = month.abb[as.numeric(Month)])

Is there a quick way to replace column values in R?

You can use sprintf:

# create the example used by the OP
dat <- data.frame(Temperature = 31:33,
Height = c(157, 159, 139))

# use sprintf along with seq_len
dat$Height <- sprintf("pic_%05d", seq_len(NROW(dat)))

# show the result
dat
#R> Temperature Height
#R> 1 31 pic_00001
#R> 2 32 pic_00002
#R> 3 33 pic_00003

You can change the 05d if you want more leading zeros. E.g. 07d will give a seven digit sequence. The manual page for sprintf have further details.

How to replace numbers with dates or remove rows with numbers from data frame?

Assuming that we have a 'dates' column in the data.frame which includes only two formats - 1) %m/%d/%Y and 2) numeric values - then we apply the as.Date with the format %m/%d/%Y on the whole 'dates'. If the values are numeric, it will return NA, which we do separately and update in base R

dates1 <- with(df1, as.Date(dates, format = "%m/%d/%Y"))
dates1[is.na(dates1)] <- with(df1, as.Date(as.integer(dates[is.na(dates1)]),
origin = "1899-12-30"))
df1$dates <- dates1

-output

> df1$dates
[1] "2006-02-12" "2006-03-12" "2006-04-12" "2006-05-12" "2006-06-12" "2006-07-12" "2006-08-12" "2006-09-12" "2006-10-12" "2006-11-12" "2006-12-12" "2006-12-13"
[13] "2006-12-14" "2006-12-15" "2006-12-16" "2006-12-17" "2006-12-18" "2006-12-19" "2006-12-20" "2006-12-21" "2006-12-22" "2006-12-23" "2006-12-24" "2006-12-25"
[25] "2006-12-26" "2006-12-27" "2006-12-28" "2006-12-29" "2006-12-30" "2006-12-31"

data

df1 <- structure(list(dates = c("38760", "38788", "38819", "38849", 
"38880", "38910", "38941", "38972", "39002", "39033", "39063",
"12/13/2006", "12/14/2006", "12/15/2006", "12/16/2006", "12/17/2006",
"12/18/2006", "12/19/2006", "12/20/2006", "12/21/2006", "12/22/2006",
"12/23/2006", "12/24/2006", "12/25/2006", "12/26/2006", "12/27/2006",
"12/28/2006", "12/29/2006", "12/30/2006", "12/31/2006")),
class = "data.frame", row.names = c(NA,
-30L))

Replace all particular values in a data frame

Like this:

> df[df==""]<-NA
> df
A B
1 <NA> 12
2 xyz <NA>
3 jkl 100


Related Topics



Leave a reply



Submit