Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add characters to a numeric column in dataframe

I have a dataframe like this:

  V1      V2      V3 
1  1 3423086 3423685 
2  1 3467184 3467723 
3  1 4115236 4115672 
4  1 5202437 5203057 
5  2 7132558 7133089 
6  2 7448688 7449283 

I want to change the V1 column and add chr before the number. Just like this:

  V1      V2      V3 
1  chr1 3423086 3423685 
2  chr1 3467184 3467723 
3  chr1 4115236 4115672 
4  chr1 5202437 5203057 
5  chr2 7132558 7133089 
6  chr2 7448688 7449283 

Is there a way to do this in R?

like image 507
Lisann Avatar asked Nov 09 '11 12:11

Lisann


People also ask

How do I add a character to a column in pandas?

Append a character or string to start of the column in pandas: Appending the character or string to start of the column in pandas is done with “+” operator as shown below.

How do I add characters to a column in R?

To add a new column to a dataframe in R you can use the $-operator. For example, to add the column “NewColumn”, you can do like this: dataf$NewColumn <- Values . Now, this will effectively add your new variable to your dataset.

How do you add a value to a column in a DataFrame?

append() function is used to append rows of other dataframe to the end of the given dataframe, returning a new dataframe object. Columns not in the original dataframes are added as new columns and the new cells are populated with NaN value. Parameters: other : DataFrame or Series/dict-like object, or list of these.


1 Answers

The regex pattern "^" (outside any character-class brackets) represents the point just before the first character of a "character"-class item (aka "string" in other computer languages). This just replaces the beginning of each "character" element in vector with a stem of "chr". It implicitly coerces a "numeric" input value to "character" so alters the mode of the result.

> dat$V1 <- sub("^", "chr", dat$V1 )
> dat
    V1      V2      V3
1 chr1 3423086 3423685
2 chr1 3467184 3467723
3 chr1 4115236 4115672
4 chr1 5202437 5203057
5 chr2 7132558 7133089
6 chr2 7448688 7449283

Could, of course, have used paste("chr", dat$V1, sep=""), but I thought a regex solution might be neater.

like image 147
IRTFM Avatar answered Oct 23 '22 03:10

IRTFM