Replacing Values in a Column with Another Column R

replacing values in a column with another column R

You can use merge to match by id, then replace in column swl1 those items from datB which exist:

datC <- merge(datA, datB, all.x=TRUE)
datC
## id swl1 swl2
## 1 1 0.8 0.8
## 2 2 0.7 NA
## 3 3 0.4 0.6
## 4 4 0.7 NA
## 5 5 0.0 0.7

This matches up the rows. Now to replace those values in column swl1 with the non-NA values from column swl2:

datC$swl1 <- ifelse(is.na(datC$swl2), datC$swl1, datC$swl2)
datC$swl2 <- NULL
datC
## id swl1
## 1 1 0.8
## 2 2 0.7
## 3 3 0.6
## 4 4 0.7
## 5 5 0.7

Replace values in one column by taking values from another column

You can group by name1 and if name1 and name4 are equal replace the name4 value with 1st non-NA value available.

library(dplyr)

df %>%
group_by(name1) %>%
mutate(name4 = ifelse(name1 == name4, na.omit(unlist(cur_data()))[1], name4)) %>%
ungroup

# name1 name2 name3 name4
# <chr> <chr> <chr> <chr>
#1 a x NA x
#2 b NA b1 b1
#3 c NA c1 c1
#4 a NA NA x

Replacing values in data frame column based on another column

ifelse is vectorize. We can use ifelse without using a loop.

dat$e <- ifelse(dat$d == -Inf, NA, dat$e)

DATA

dat <- read.table(text = "a  b  c     d   e
1 2 3 23 1
4 5 6 -Inf 2
7 8 9 2 8
10 11 12 -Inf NaN", header = TRUE)

In R, how to replace values in a column with values of another column of another data set based on a condition?

A dplyr solution. There's probably an elegant way with less steps.

library(dplyr)

target_df <- target_df %>%
left_join(registry_df,
by = c("project_name" = "to_change")) %>%
mutate(replacement = ifelse(is.na(replacement), project_name, replacement)) %>%
select(project_name = replacement, sum)

Result:

# A tibble: 4 × 2
project_name sum
<chr> <chr>
1 Duck 4307
2 Tank 9567
3 Delorean 5344
4 Parix 1043

Replace whole values in a column with another column using condition

I think you want to merge the two datasets using the level. Then for each date you would have variable and depth, and you can do what you want with that (including overwriting the level with the depth, as you suggest).

library(tidyverse)
reference_data <- read_table("depth levels
0.06 1
0.19 2
0.33 3
0.48 4
0.63 5
0.8 6")

data_file <- read_table("Date Levels variable
3-Jan 1 15.25
3-Feb 5 13.09
3-Mar 25 14.21
3-Apr 26 13.65
3-May 27 12.79
3-Jun 27 15.65")

data_file %>%
left_join(reference_data, by = c("Levels" = "levels"))
#> # A tibble: 6 x 4
#> Date Levels variable depth
#> <chr> <dbl> <dbl> <dbl>
#> 1 3-Jan 1 15.2 0.06
#> 2 3-Feb 5 13.1 0.63
#> 3 3-Mar 25 14.2 NA
#> 4 3-Apr 26 13.6 NA
#> 5 3-May 27 12.8 NA
#> 6 3-Jun 27 15.6 NA

Created on 2021-07-14 by the reprex package (v2.0.0)

Replace value in one column with value from another column in the same row with certain condition

Credits to Darren Tsai. Using his solution we could use A+B instead of pmax because of the 0:

With this dataframe:

df <- structure(list(A = c(0L, 0L, 123L), B = c(123L, 123L, 0L), Dict = c("B", 
"A", "A")), class = "data.frame", row.names = c(NA, -3L))
library(dplyr)
df %>%
mutate(across(A:B, ~ ifelse(Dict == cur_column(), A+B, 0)))

I get this:

    A   B Dict
1 0 123 B
2 123 0 A
3 123 0 A

Replace value in column with corresponding value from another column in same dataframe

There are several alternatives. Here are three:

Most basic, using data.frames :

df[ df$X1 == "a" , "X1" ] <- df[ df$X1 == "a", "X2" ]

More Terse, using with:

df$X1 <- with( df, ifelse( X1 == "a", X2, X1 ) )

Most terse and transparent Using data.tables

library(data.table) ## >= 1.9.0
setDT(df) ## converts to data.table by reference, no need for `<-`

df[ X1 == "a", X1 := X2 ]

Replace values of a column based on another column having as input a dataframe

We don't need any mapas 'a', 'b' and the number of rows of 'df' have the same length. So, an elementwise comparison with == can be done. Instead of replace, it may be better with ifelse/case_when etc as replace values should be of the same length as the list condition TRUE elements

library(dpyr)
df %>%
mutate(z = case_when(a == y ~ as.integer(b), TRUE ~ z))

-output

# A tibble: 3 x 2
# y z
# <int> <int>
#1 10 100
#2 11 200
#3 12 22

Or using base R

df$z <- with(df, ifelse(a == y, b, z))

In the OP's code, there is a difference in length when we do

 replace(x = z, y == .x, values = .y)

where 'z' will be the full column length, .x , .y will be each row element

Update

Based on the updated data, we could a join and then use coalesce

df %>% 
left_join(val, by = c('y' = 'a')) %>%
transmute(y, z = coalesce(b, z))
# A tibble: 4 x 2
# y z
# <dbl> <dbl>
#1 10 100
#2 11 21
#3 12 200
#4 13 23

Replacing values from a column using a condition in R

# reassign depth values under 10 to zero
df$depth[df$depth<10] <- 0

(For the columns that are factors, you can only assign values that are factor levels. If you wanted to assign a value that wasn't currently a factor level, you would need to create the additional level first:

levels(df$species) <- c(levels(df$species), "unknown") 
df$species[df$depth<10] <- "unknown"


Related Topics



Leave a reply



Submit