I want to get rid of "-n" at the end of every word in my data frame. Is there any way to do this in R?
Asked
Active
Viewed 156 times
-2
-
1You say every word in your data frame. Does your data frame consist of just words or are they multi-word strings line "test-n string-n"? – G5W Jun 13 '17 at 15:02
-
try look at [this](https://stackoverflow.com/questions/14871249/can-i-use-gsub-on-each-element-of-a-data-frame) – simone Jun 13 '17 at 15:06
-
I tried to add image but it failed.. there is a column and data is like "end-n, firing-n, game-n" – sb10 Jun 13 '17 at 15:09
-
Try `dput(head(df$TextColumn, 20))` and paste the results into your question. – G5W Jun 13 '17 at 15:15
-
I'm sorry but what does "df" mean..? – sb10 Jun 13 '17 at 15:18
-
`sub('-.*', '', 'some-n')` – Sotos Jun 13 '17 at 15:21
2 Answers
3
gsub('-n$','', 'my_word-n')
[1] "my_word"
#to make this work for the whole data frame you can do
my_new_df <- sapply(my_old_df, function(x) gsub('-n$','', x))

Mouad_Seridi
- 2,666
- 15
- 27
0
To remove the unwanted string from the end of every word, try
gsub('-n\\b','', df$TextColumn)

G5W
- 36,531
- 10
- 47
- 80
-
If you want to save the result , you need something like `df$TextColumn = gsub('-n\\b','', df$TextColumn)` – G5W Jun 13 '17 at 15:39