Replacing a whole column in R
With assignment:
data$B <- whatever
# or
data[, "B"] <- whatever
# or
data[["B"]] <- whatever
Replace all occurrences of a string in a data frame
If you are only looking to replace all occurrences of "< "
(with space) with "<"
(no space), then you can do an lapply
over the data frame, with a gsub
for replacement:
> data <- data.frame(lapply(data, function(x) {
+ gsub("< ", "<", x)
+ }))
> data
name var1 var2
1 a <2 <3
2 a <2 <3
3 a <2 <3
4 b <2 <3
5 b <2 <3
6 b <2 <3
7 c <2 <3
8 c <2 <3
9 c <2 <3
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
R: How do I replace a column with another column from a different dataframe?
Using base R
,
ped[,6] = bipolar_ctl$Phenotype
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"
Replace all row values according to a value in a specific column in R
We can use across
in dplyr
- loop across
the 'c2', 'c3' columns, and use the logical column from 'c1' to return the values of the column, by default the last condition i.e. TRUE
will be all NA
library(dplyr)
data <- data %>%
mutate(across(c2:c3, ~ case_when(c1 ~ .x)))
-output
data
# A tibble: 4 × 4
var c1 c2 c3
<chr> <lgl> <lgl> <lgl>
1 a TRUE TRUE TRUE
2 b FALSE NA NA
3 c TRUE TRUE TRUE
4 d FALSE NA NA
Replace matching column values in multiple dataframes with NA in R
As you have the data frames in this format, you can set the values of the required data to NA
in the new data frames, and then update the rows of the original data frames with these values using dplyr::rows_update()
. This assumes you have at least dplyr
v1.0.0.
library(dplyr)
outliers1$eucDist <- NA
outliers2$eucDist2 <- NA
dat |>
rows_update(outliers1, by = c("participant", "target")) |>
rows_update(select(outliers2, -eucDist), by = c("participant", "target"))
# participant target eucDist eucDist2
# 1 01 1 NA NA
# 2 01 2 0.16 0.04
# 3 01 3 0.89 0.03
# 4 02 1 0.10 0.05
# 5 02 2 0.11 1.45
# 6 02 3 NA 0.09
Related Topics
Ggplot2: Shape, Color and Linestyle into One Legend
Could Not Find Function Tagpos
Netlogo - Misalignment with Imported Gis Shapefiles
How to Do Histograms of This Row-Column Table in R Ggplot
R: Gradient Fill for Geom_Rect in Ggplot2
Line Segments or Rectangles with Hover Information in R Plotly Figure
Axis-Labeling in R Histogram and Density Plots; Multiple Overlays of Density Plots
Data.Table Objects Aren't Updated in Rstudio Environment Panel
Pipe in Magrittr Package Is Not Working for Function Load()
What Does The "More Columns Than Column Names" Error Mean
Split Violin Plot with Ggplot2 with Quantiles
Cannot Install R Tseries, Quadprog ,Xts Packages in Linux
R: How to Count How Many Points Are in Each Cell of My Grid
How to Calculate Euclidean Distance Between Two Matrices in R
R - Stuck with Plot() - Colouring Shapefile Polygons Based Upon a Slot Value