This is a test version of Biostars. For the public version, visit https://www.biostars.org.
insert word in each row

Hello,

I have this dataframe

chrname     start       end
1       X  99883667  99894988
2       X  99839799  99854882
3      20  49551404  49575092
4       1 169818772 169863408
5       1 169631245 169823221
6       1  27938575  27961788

and I would like to add in the first column chrname a word "chr" before each word

chrname     start       end
1    chr X  99883667  99894988
2    chrX  99839799  99854882
3    chr  20  49551404  49575092
4    chr 1 169818772 169863408
5    chr  1 169631245 169823221
6     chr 1  27938575  27961788

I tried with paste and assign but no result?any help please?

assign r paste

4 answers

Let say your dataframe is d

d$chrname<-paste("chr", d$chrname, sep="")

You said before each word, but your result show that some of them have extra blank but others not

I think he just want to append 'chr' to each chromosome as few tools expects data that way.

awk -F '\t' '{$2=chr$2;print $0}' filename

Didn't test that :)

Let d be your dataframe.

d_new=data.frame(chrname=paste(rep("chr",dim(d)[1]),d$chrname,sep=""),start=d$start,stop=d$stop)

Should do the job.

These are very basic steps with R. Please go through the R documentation http://cran.r-project.org/manuals.html

These will help you a lot.

Also, always use "Tab" key while working with the function, which will let you know the parameters you could provide.

or ?function_name

Log in to answer this question.