[R] Help with apply and new column?
Sariya, Sanjeev
ss5505 at cumc.columbia.edu
Mon Mar 5 20:06:36 CET 2018
Thanks. I think nabble is good for programming questions. Bear with me if I'm incorrect.
Data: Genomics SNP information
Goal: I need to add Chromosome and SNP position to the data frame I'm using through apply.
I'd like to add new column from text processed through apply function.
For example: 10:60523:T:G (Column 2)
CHR: 10
Position: 60523
Dataset:
chr rs ps n_miss allele1 allele0 af beta se l_remle p_wald
-9 10:60523:T:G -9 0 T G 0.977 -1.769354e-02 3.597196e-02 1.566731e-01 6.228309e-01
-9 10:60684:A:C -9 0 A C 0.973 1.698925e-02 2.942366e-02 1.561001e-01 5.636926e-01
-9 10:61331:A:G -9 0 A G 0.973 1.708586e-02 2.942424e-02 1.560944e-01 5.614851e-01
-9 10:62010:C:T -9 0 C T 0.980 -8.513143e-03 3.837054e-02 1.566875e-01 8.244260e-01
Code:
--------------------------------------------------------
data<-read.table("small.txt",header = T) # read data
data<-data[,c(2,11)] #delete other columns not needed
#--split data on : and get chromosome and position
split_rs<-function(rs){
chr<-vector(,length(rs)) # create new vector to store chr
pos<-vector(,length(rs)) #create new vector to store position
for(i in 1:length(rs)){ #iterate over RS column
if(grepl(":",rs[i])){ #if : in column string
temp <- strsplit(rs[i],":",fixed=T) #split
chr[i] <-temp[[1]][1] #store CHR
pos[i] <- temp[[1]][2] #store position
}
}
return(list(chr=chr,pos=pos)) #return making a list
}
data$POS<-"NA" #add new column CHR and make NA
data$CHR <- "NA" #add new column POS and make NA
temp<-apply(data,2,split_rs) #send data frame to function
#--I assign value from list sent -- I would like to improve this part
data$CHR<-temp$rs$chr
data$POS<-temp$rs$pos
rm(temp)
colnames(data)<-c("SNP","P","CHR","BP")
--------------------------------------------------------
-----Original Message-----
From: Jeff Newmiller [mailto:jdnewmil at dcn.davis.ca.us]
Sent: Monday, March 5, 2018 1:48 PM
To: r-help at r-project.org; Sariya, Sanjeev <ss5505 at cumc.columbia.edu>; R Help <r-help at r-project.org>
Subject: Re: [R] Help with apply and new column?
Read the Posting Guide... (see message footer) ... some relevant things you can find there:
a) Yes, this appears to be about how to use an R base function so it is on topic
b) Post a reproducible example (include some sample data, preferably using the dput function)
c) Post using plain text so the mailing list doesn't convert it for you and mangle things in a way you did not intend.
--
Sent from my phone. Please excuse my brevity.
On March 5, 2018 10:07:24 AM PST, "Sariya, Sanjeev" <ss5505 at cumc.columbia.edu> wrote:
>Hello members,
>
>Can I ask question for apply, adding new column to data frame on this
>e-mail list?
>
>Thanks!
>
>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list