Re: [R] Replacing values in dataframes

2009-09-21 Thread Monna Nygård

Thank you so much for trying to help me. 

 

Thus, I still can't get it to work. 

 

I will clearify a bit. If  you somehow have time helping me I would much 
appreciate it.

 

NAD and Prot.amount are both data.frames. There are several different 
dataframes called NAD,NAD1, NAD2 etc. that I would like to run the loop over. 
Prot.amount has all the samplenames as its row.names and the correct values as 
Prot.amount[,1]. Thus, the NADs are data.frames with values for the samples, 
they DO NOT contain all the samples.

 

> NAD[1:3,1:3]
Sample.Id Main.abs..1 Main.abs..2
148 10a 0.04836 0.04994
167 11a_1109 0.32245 0.36541
173 11b_1109 0.29293 0.32815

 

 

> Prot.amount[1:3,1]
> 10a 11a_1109 11b_1109
15.516 38.248 42.297

 

> dim(Prot.amount)
[1] 30  1
> dim(NAD)
[1] 23 12

 

The thing I want to do is to replace the sample name with the correct 
Prot.amount.  So that in all dataframes where I have for instance sample 10a i 
want to have a column with its corresponding prot.amount, here 15.516.

 

 

Sample.id=row.names(Prot.amount)

gives a vector with all the correct sample.id's

 

new.id=Prot.amount[1:nrow(Prot.amount),] gives all the samples and their 
correct prot.amounts. 

 

> new.id[1:8]
 10a 11a_1109 11b_1109 12a_1109 12b_1109  1a_1109   2a  2a_1009 
  15.516   38.248   42.297   36.134   25.467   28.1849.9272.242 

 

 iddf <- data.frame((Sample.id=row.names(Prot.amount)), 
new.id=Prot.amount[1:nrow(Prot.amount),])

 

Thus, 

newNAD <- merge( NAD, iddf) 

> newNAD[4,]
  Sample.Id Main.abs..1 Main.abs..2 Main.abs..3 Main.abs..4 Main.abs..5 
Main.abs..6 Main.abs..7 Main.abs..8 Main.abs..9
4  12a_1109 0.26291 0.26794 0.27809 0.28948 0.29654  
0.3051 0.31388  0.3223 0.33066
  Main.abs..10 Main.abs..11 X.Sample.id...row.names.Prot.amount.. new.id
4  0.33806  0.34577   10a 15.516

 

Produces the correct looking dataframe, only that the first sample.id and 
corresponding Prot.amount gets merged to all rows. 

 

So what I need to do is a loop. This is my suggestion: 

 

Changing<-function(A){

tmp<-mat.or.vec(nr=nrow(A),nc=1)
newNAD <-mat.or.vec(nr=nrow(A),nc=1)
 
for (j in 1:nrow(A)) {
tmp[j] <- data.frame((A$Sample.id=row.names(Prot.amount)), 
(A$new.id=Prot.amount[j,]))
 newNAD <- merge(A[j,], tmp[j])
 }
 newNAD
}

 

Thus, of course it doesn't work that easily. This is becouse 

> Changing(NAD)
Error in `$<-.data.frame`(`*tmp*`, "Sample.id", value = c("10a", "11a_1109",  : 
  replacement has 30 rows, data has 23

 

meaning the dataframe NAD with the samples have 23 samples, the Prot.amounts 
file have 30 different samples and their corresponding values. How do I include 
into the loop that I only want to replace those Prot.amount values found in the 
NAD dataframe? 

 

Thank you so much!


 
> Date: Sat, 19 Sep 2009 21:35:31 -0700
> To: monn...@hotmail.com; r-help@r-project.org
> From: m...@llnl.gov
> Subject: Re: [R] Replacing values in dataframes
> 
> What I would probably do is along these lines:
> 
> iddf <- data.frame(Sample.id=names(Prot.amount), new.id=Prot.amount[1,])
> 
> newNAD <- merge( NAD, iddf)
> 
> This is not tested, but it looks right to me, 
> assuming I understand the structure of what 
> you're trying to do.
> 
> I'm also assuming that NAD has more than three 
> rows, and that Prot.amount has as many columns as 
> NAD has rows. And that you just showed us the 
> first three rows of NAD and first three columns 
> of Prot.amount in order to keep the email simple.
> 
> One final note ... if Prot.amount is an object 
> within R, it is *not* a file. You may have read 
> it in from a file, of course, but it isn't a file 
> inside R. I'm assuming it's a dataframe.
> 
> -Don
> 
> At 1:18 PM +0300 9/19/09, Monna Nygård wrote:
> >Hi,
> >
> >
> >
> >This is a question of a newbie getting into the exciting world of R.
> >
> >
> >
> >I have several dataframes in the same format as NAD:
> >
> >
> >
> >
> >
> >> NAD[1:3,1:3]
> >
> > Sample.Id Main.abs..1 Main.abs..2
> >148 10a 0.04836 0.04994
> >167 11a_1109 0.32245 0.36541
> >173 11b_1109 0.29293 0.32815
> >
> >
> >What I want to do is to replace the Sample.Id 
> >with a corresponding number.The number i have in 
> >another file,called Prot.amount
> >
> >
> >
> >> Prot.amount[1:3,1]
> > 10a 11a_1109 11b_1109
> > 15.516 38.248 42.297
> >
> >
> >
> >
> >
> >> row.names(NAD)<-(NAD[,1])
> >> NAD$Sample.Id <- repla

[R] Replacing values in dataframes

2009-09-19 Thread Monna Nygård

Hi,

 

This is a question of a newbie getting into the exciting world of R. 

 

I have several dataframes in the same format as NAD:

 

 

> NAD[1:3,1:3]

Sample.Id Main.abs..1 Main.abs..2
148   10a 0.04836 0.04994
167  11a_1109 0.32245 0.36541
173  11b_1109 0.29293 0.32815


What I want to do is to replace the Sample.Id with a corresponding number.The 
number i have in another file,called Prot.amount

 

> Prot.amount[1:3,1]
 10a 11a_1109 11b_1109 
  15.516   38.248   42.297 

 

 

> row.names(NAD)<-(NAD[,1])
> NAD$Sample.Id <- replace(NAD$Sample.Id, 
> NAD$Sample.Id=="10a",Prot.amount["10a",1])

> NAD[1:3,1:3]
 Sample.Id Main.abs..1 Main.abs..2
10a 15.516 0.04836 0.04994
11a_1109  11a_1109 0.32245 0.36541
11b_1109  11b_1109 0.29293 0.32815

 

So what I have tried to do is to write a function that would allow me to 
replace the values automatically of all dataframes. This I just can't get to 
work.  

 

Thank you so much in advance!
  
_
[[elided Hotmail spam]]

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


[R] colouring a tree

2008-08-08 Thread Monna Nygård




Hi all.I would be very pleased if someone could help me,as I do not seem to get 
the different branches of my tree painted in different colours. The closest I 
get is colouring the names of my samples(=names of the branches). Here is the 
code.data <- read.table(file = "S://SEDIM//TRFLP//B12.5_50-700bp.txt" , as.is = 
TRUE, header = TRUE, sep = "\t", row.names = 1) #define vector with depths of 
samples row.names(data)<- 
c("119","175","135","330","51","422","67.0","409","470.0","70","67","89","134.5","215","330.0","409.0","470","50.5","80","119.0","174.5","222","301","421.5","280","171","256","243","404","37","157","28","187.0","79.5","42.0","283","261","85","147","204.0","235","411.0","514","77","204","87","366","306","351","371","38","534","199","407","42","167","480","195","22","35","80.0","433","43","109","214","363","292","61","115","178","273","521","72","126","253","288","501","82.5","113","250","359.0","498","19","130","389","324","24","58","124","388","319.0","164.0","101.0","153","383","345","219","179","161","375","298","450.0","555","439","54","54.5","490","465","411","18.0","85.0","503","455","394","179.0","187","416","447","219.0","461","164","366.0","474","167.0","236","507","319","509","467","507.5","450","359","507.0","192","453","101","456","512","517")depths
 <- 
c("119","175","135","330","51","422","67.0","409","470.0","70","67","89","134.5","215","330.0","409.0","470","50.5","80","119.0","174.5","222","301","421.5","280","171","256","243","404","37","157","28","187.0","79.5","42.0","283","261","85","147","204.0","235","411.0","514","77","204","87","366","306","351","371","38","534","199","407","42","167","480","195","22","35","80.0","433","43","109","214","363","292","61","115","178","273","521","72","126","253","288","501","82.5","113","250","359.0","498","19","130","389","324","24","58","124","388","319.0","164.0","101.0","153","383","345","219","179","161","375","298","450.0","555","439","54","54.5","490","465","411","18.0","85.0","503","455","394","179.0","187","416","447","219.0","461","164","366.0","474","167.0","236","507","319","509","467","507.5","450","359","507.0","192","453","101","456","512","517")
 #function for labelling plotting figures MyFun <- function(x){  if (x < 100) { 
x <- 1} if (x > 100 & x < 200){x <- 2 } if(x > 200 & x < 300){ x<- 3 } if (x > 
300 & x < 400){ x<- 4} if (x > 400 & x < 500){x<- 5 } if (x > 500){ x <- 6 } 
return((x)) }#apply the function to the datasetsapply(depths, 
MyFun)->symbols#create the vectors for the 
plotfiguresas.vector(symbols)->symbols 
library(ape)col.vec<-symbolsplot(as.phylo(hclust(dist((data,tip.color=col.vec,main="
 Hierarchical cluster analysis ")  I need to change the tip.color into 
something else, but what?Even better would be if someone could help me changing 
the colours given according to the depth of the sample (according to how high a 
number I have given the samples in row.names), I mean getting darker as the 
samples get deeper etc.. Is there an easy way doing this?Thank you so much in 
advance,all help appreciated.Regards, Yvonne 

Vårkänslor? Hitta någon att dela dem med! MSN Dejting 
_
[[elided Hotmail spam]]

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


[R] colouring a tree

2008-08-07 Thread Monna Nygård

Hi all.

I would be very pleased if someone could help me, as I do not seem to get the 
different branches of my tree painted in different colours. The closest I get 
is colouring the names of my samples(=names of the branches). Here is the code.






data <- read.table(file = "S://SEDIM//TRFLP//B12.5_50-700bp.txt" , as.is = 
TRUE, header = TRUE, sep = "\t", row.names = 1)#define vector with depths of 
samples









row.names(data)<-



 
c("119","175","135","330","51","422","67.0","409","470.0","70","67","89","134.5","215","330.0","409.0","470","50.5","80","119.0","174.5","222","301","421.5","280","171","256","243","404","37","157","28","187.0","79.5","42.0","283","261","85","147","204.0","235","411.0","514","77","204","87","366","306","351","371","38","534","199","407","42","167","480","195","22","35","80.0","433","43","109","214","363","292","61","115","178","273","521","72","126","253","288","501","82.5","113","250","359.0","498","19","130","389","324","24","58","124","388","319.0","164.0","101.0","153","383","345","219","179","161","375","298","450.0","555","439","54","54.5","490","465","411","18.0","85.0","503","455","394","179.0","187","416","447","219.0","461","164","366.0","474","167.0","236","507","319","509","467","507.5","450","359","507.0","192","453","101","456","512","517")

 depths <- 
c("119","175","135","330","51","422","67.0","409","470.0","70","67","89","134.5","215","330.0","409.0","470","50.5","80","119.0","174.5","222","301","421.5","280","171","256","243","404","37","157","28","187.0","79.5","42.0","283","261","85","147","204.0","235","411.0","514","77","204","87","366","306","351","371","38","534","199","407","42","167","480","195","22","35","80.0","433","43","109","214","363","292","61","115","178","273","521","72","126","253","288","501","82.5","113","250","359.0","498","19","130","389","324","24","58","124","388","319.0","164.0","101.0","153","383","345","219","179","161","375","298","450.0","555","439","54","54.5","490","465","411","18.0","85.0","503","455","394","179.0","187","416","447","219.0","461","164","366.0","474","167.0","236","507","319","509","467","507.5","450","359","507.0","192","453","101","456","512","517")#function
 for labelling plotting figures

MyFun <- function(x){

  if
(x < 100) {

   x
<- 1

}

  if
(x > 100 & x < 200){

   x
<- 2

} 

  if
(x > 200 & x < 300){

   x
<- 3

} 

  if
(x > 300 & x < 400){

   x
<- 4

} 

  if
(x > 400 & x < 500){

   x
<- 5

} 

  if
(x > 500){

   x
<- 6

  }

  return((x))

}




#apply the function to the dataset

sapply(depths, MyFun)->symbols

 

#create the vectors for the plotfigures

as.vector(symbols)->symbols



library(ape)

col.vec<-symbols



plot(as.phylo(hclust(dist((data,tip.color=col.vec,main=" Hierarchical
cluster analysis ") I need to change the tip.color into something else, but 
what?Even better would be if someone could help me changing the colours given 
according to the depth of the sample (according to how high a number I have 
given the samples in row.names), I mean getting darker as the samples get 
deeper etc.. Is there an easy way doing this?

Thank you so much in advance,all help appreciated.
Regards, Yvonne
 

_
[[elided Hotmail spam]]

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


[R] PCA analysis

2008-06-17 Thread Monna Nygård

Hi,
 
I have a problem with making PCA plots that are readable. 
I would like to set different sympols instead of the numbers of my samples or 
their names, that I get plotted (xlabs). 
How is this possible? With points, i don´t seem to get the right data plotted 
onto the PCA plot, as I do not quite understand from where it is taken. I dont 
know how to 
plot the correct columns of the prcomp outcome (p). 
I would really appreciate if someone could help me, I have struggled with this 
for days now. How can I make a function that gives different symbols 
for the points, depending on how big the number given to it as xlabs is?
 
Making the plots.
 
read.table(file = "S:\\SEDIM\\TRFLP\\B90-700.txt",sep="\t", header=T)->boutbout 
<-bout[-1]p <- prcomp(bout)
biplot(p, choices = c(2,3), scale = 1, pc.biplot = FALSE, var.axes = F, ylabs = 
NULL, 
xlabs=c("119","175","135","330","51","422","67","409","470","70","67","89","135","215","330","409","470","51","80","119","175","222","301","422","280","171","256","243","404","37","157","28","187","70","42","283","261","85","147","204","235","411","514","77","204","87","366","306","351","371","38","534","199","407","42","167","480","195","22","35","80","433","43","109","214","363","292","61","115","178","273","521","72","126","253","288","501","83","113","250","359","498","19","130","389","324","24","58","124","388","319","164","101","153","383","345","219","179","161","375","298","450","555","439","54","54","490","465","411","18","85","503","455","394","179","187","416","447","219","461","164","366","474","167","236","507","319","509","467","507","450","359","507","192","453","101","456","512","517"),
 cex=0.67, main="90-700bp")
 
_
[[elided Hotmail spam]]

PLink
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


[R] i-best, grep function

2008-05-07 Thread Monna Nygård

Hi,

I'm trying to use the i-best software. Does anyone have experience from this, I 
can't get it to work with my data.

Here is the code: 

T1 <- read.delim(file="S://SEDIM//Yvonne//2_5//T1.txt",col.names= 
c("Dye/Sample_Peak", "Sample_File_Name", "Size", "Height", "Area_in_Point", 
"Area_in_BP", "Data_Point", "Begin_Point","Begin_BP", "End_Point", 
"End_BP", "Width_in_Point", "Width_in_BP", "User_Comments", "User_Edit"))
T1 <- subset(T1, Size < 1000 & Size > 50)
T1.B <- cbind(T1[grep("^B", as.character(T1$Color),perl=T),3],T1[grep("^B", 
as.character(T1$Color),perl=T),5])
T1.B <- cbind(T1.B, T1.B[,2]/sum(T1.B[,2]))


It works alright until the last two lines.  I try to grep the columns 3 and 5, 
but the outcome is 
 T1.B
 [,1] [,2]. 
I don´t quite understand the code of as.character(t1$Color), perl=T. Could 
anyone please explane what this does, I havn´t found it anyware and I am so 
stuck. Maybe I'm doing something else wrong as well..

I would really appreciate some help, thank you.

Yvonne


_


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


[R] removing rows from matrix

2008-05-02 Thread Monna Nygård

 Hi, I have a problem regarding matrix handeling. I am working with a serie of 
matrixes containing several columns. Now I would like to delete those rows of 
the matrixes,that in one of the columns contain values less than 50 or greater 
than 1000. How would this be possible, I have tried to create a simple function 
for this, but I just don't seem to get it right.Thank you so much for your 
help, Monna  
_
[[elided Hotmail spam]]

PLink
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


[R] missing values in document

2008-04-30 Thread Monna Nygård

No matter how I´ve tried o find a solution for this simple question, I can´t. 
Sorry for bothering with such a matter.
 
I have an excel-files with some empty cells=missing values. How do I tell R 
that these should be NA´s? 
 
>  TRFLP1 <-(read.table(file="S://SEDIM//Kokeilu//TRFLP1.txt",col.names= 
> c("Dye_Sample Peak", "Sample_File_Name", "Size", "Height", "Area_in_Point", 
> "Area_in_BP", "Data_Point", "Begin_Point", "Begin_BP", "End_Point", "End_BP", 
> "Width_in_Point", "Width_in_BP", "1" ), header=F , dec = ",", )Error in 
> scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :   line 
> 1743 did not have 14 elements
 
I believe the error occurs becouse line 1743 has an empty cell. How do I read 
in the file without first checking for missing values and changing them to NAs 
myself?
 
Thank you for you time,
 
Monna Nygård
_


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.