Reputation: 1373
One of my column in my datafra looks like this:
[1] "MIT" "MIT" "NUC" "MIT" "CYT" "MIT" "NUC" "MIT" "CYT" "NUC" "NUC" "CYT" "MIT" "MIT" "" "" "CYT"
[18] "NUC" "NUC" "NUC" "CYT" "CYT" "CYT" "CYT" "CYT" "MIT" "CYT" "CYT" "MIT" "ME1" "NUC" "MIT" "CYT" "NUC"
[35] "ME1" "EXC" "MIT" "CYT" "MIT" "MIT" "ME2" "ME2" "" "" "ME3" "VAC" "NUC" "ME2" "EXC" "VAC" "ME3"
I need to change "ME3" for ME.
When I used this code:
df_norm_pre$MIT["ME1"]="ME"
I find this error:
Error in
$<-.data.frame
(*tmp*
, MIT, value = c("MIT", "MIT", "NUC", : replacement has 1550 rows, data has 1549.
Could someone help me with this problems?
Upvotes: 0
Views: 92
Reputation: 887881
We can use replace
df_norm_pre$MIT <- replace(df_norm_pre$MIT, df_norm_pre$MIT == "ME3", "ME")
Or just assignment
df_norm_pre$MIT[df_norm_pre$MIT == "ME3"] <- "ME"
Or with sub
df_norm_pre$MIT <- sub("^ME\\d+$", "ME", df_norm_pre$MIT)
Error in OP's code is because the indexing is not correct
df_norm_pre$MIT["ME1"]
The df_norm_pre$MIT
extracts the column as a vector
, then using ["ME1"]
, expects that is either a column with name of "ME1" or a list
with that name. Both of these are not TRUE here. Instead, we can use logical index to subset df_norm_pre$MIT == "ME1"
Upvotes: 1
Reputation: 102700
Maybe you can try gsub
to see if it can do the replacement
df_norm_pre <- within(df_norm_pre, MIT <- gsub("ME3","ME",MIT))
or use replace
like
df_norm_pre <- within(df_norm_pre, MIT <- replace(MIT, MIT == "ME3", "ME"))
Upvotes: 1