How to replace only the trailing character of several variable names in R?

Below is some background information about my dataset, if you want to understand where my question comes from (I really want to combine the datasets, so maybe someone knows a more efficient way).

Question: How to replace only the final character of a variable name in R with nothing (for several variables)?

I tried using the sub () function, and it worked fine, however some variable names contain a character that I want to change several times (e.g. str2tt2). I just want to “delete” or replace the last “2” with empty space.

Example: Suppose I have a data set with these variable names, and I only want to delete the last characters "_2", I tried this:

  h_2ello_2 how_2 are_2 you_2
1         1     3     5     7
2         2     4     6     8

names(data) <- sub('_2', '', names(data))

Conclusion:

  hello_2   how are you
1       1     3   5   7
2       2     4   6   8

, "_2", "h_2ello" hello_2.

- , ? !


:

. , , . 2 - A2, scoreB2, scoreC2 3, - A3, scoreB3 scoreC3.

, "2" "3" , , , .

, 2 3. : str2tt2 - Stroop 2. "2" , sub() .

+4
1

$,

names(data) <- sub('_2$', '', names(data))
names(data)
#[1] "h_2ello" "how"     "are"     "you"

OP- _2 h_2ello_2, sub _2 h_2. , .

0

Source: https://habr.com/ru/post/1683458/


All Articles