insert word in each row
4
0
Entering edit mode
9.6 years ago
yasjas ▴ 70

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 • 2.3k views
ADD COMMENT
4
Entering edit mode
9.6 years ago
Beuss ▴ 140

Let say your dataframe is d

d$chrname<-paste("chr", d$chrname, sep="")
ADD COMMENT
3
Entering edit mode
9.6 years ago
wpwupingwp ▴ 120

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

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
awk -F '\t' '{$2=chr$2;print $0}' filename

Didn't test that :)

ADD REPLY
3
Entering edit mode
9.6 years ago
michael.ante ★ 3.9k

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.

ADD COMMENT
1
Entering edit mode
9.6 years ago
Deepak Tanwar ★ 4.2k

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

ADD COMMENT

Login before adding your answer.

Traffic: 2586 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6