Reputation: 4408
I have a dataset, df, with the following values:
ID Duration
abcdefghijklmnopqrstuvwxyz 1 sec
abcdefghijklmnopqrstuvwxyz1 0 sec
abcdefghijklmnopqrstuvwxyz2 0 sec
abcdefghijklmnopqrstuvwxyz3 1 sec
abcdefghijklmnopqrstuvwxyz4 0 sec
Goal: I am plotting a histogram, and the values are just too lengthy. I would like to convert the values within the column ID to a shorter value such as:
ID Duration
A 1 sec
B 0 sec
C 0 sec
D 1 sec
E 0 sec
To do this, would I have to specify and write out every value within the row? (there are 100's of them)
rename.values(df, abcdefghijklmnopqrstuvwxyz="A")...
Upvotes: 0
Views: 6915
Reputation: 1928
You could simply create a new ID column, which would solve your issue and also preserve your original IDs (this assumes no duplicate IDs).
df <- df %>%
mutate(ID2 = 1:nrow(df)) %>%
select(ID2, Duration) # OR select(-ID) : deselects ID, keeps everything else.
Upvotes: 2
Reputation: 16178
Without the use of dplyr
, if you want to rename all values in your column ID to a shorter ID (and assuming that all of your IDs are different), you can write:
df$ID <- paste0("A",1:nrow(df))
Alternative: Using gsub
Alternatively, if you have a very long pattern that you wish to replace (such abcdef....), you can use gsub
:
df$ID <- gsub("abcdefghijklmnopqrstuvwxyz","A",df$ID)
The advantage with gsub
is that if you have an ID repeted multiple times, it will conserve this repetition because it will replace only the first part of the ID string.
Example
a <- paste0(letters[1:26], collapse = "")
df <- data.frame(ID = paste0(a,1:100),
value = rnorm(100))
So, your df
looks like:
ID value
1 A1 2.6977546
2 A2 1.9434639
3 A3 0.4191808
4 A4 -0.1545246
5 A5 2.0112518
6 A6 0.5877203
...
Now, if you replace character strings of ID
by the following command:
df$ID <- paste0("A",1:100)
or with gsub
:
df$ID <- gsub("abcdefghijklmnopqrstuvwxyz","A",df$ID)
And you get:
ID value
1 A1 2.6977546
2 A2 1.9434639
3 A3 0.4191808
4 A4 -0.1545246
5 A5 2.0112518
6 A6 0.5877203
...
So, you conserved all of your columns and values in the same order and you just modify the ID column.
Upvotes: 3