Hello, I'm trying to add a column to the following data frame. The new column will contain "black" when the 5th column(if_TE_related) is "TE_related", or "orange" when the 4th column is " " (space). "chromo" "MSU_locus" "end5" "end3" "if_TE_related" "chr04" "LOC_Os04g01006" 1032 2679 "TE_related" "chr04" "LOC_Os04g01008" 7636 3951 "TE_related" "chr04" "LOC_Os04g01010" 9521 10296 "TE_related" "chr04" "LOC_Os04g01020" 17165 17437 " " "chr04" "LOC_Os04g01030" 29372 18440 "TE_related" "chr04" "LOC_Os04g01040" 30637 37300 "TE_related" ... So, after a data manipulation, it should look like the following... "chromo" "MSU_locus" "end5" "end3" "if_TE_related" "color" "chr04" "LOC_Os04g01006" 1032 2679 "TE_related" "black" "chr04" "LOC_Os04g01008" 7636 3951 "TE_related" "black" "chr04" "LOC_Os04g01010" 9521 10296 "TE_related" "black" "chr04" "LOC_Os04g01020" 17165 17437 " " "orange" "chr04" "LOC_Os04g01030" 29372 18440 "TE_related" "black" "chr04" "LOC_Os04g01040" 30637 37300 "TE_related" "black" ... I have worked on the following code to do this job using function and loop, but it is not working. If someone help me, I would really appreciate!!! The original data frame is Os.chr4.gene.new. Gene <- Os.chr4.gene.new[, c("if_TE_related")] Genecolor <- function(Gene) { lg <-length(Gene) for(i in 1:lg) { if (Gene == "TE_related") {D1 <- (Gene == "black")} if (Gene == " ") {D1 <- (Gene == "orange")} } Gene.color <- cbind(Gene, D1) write.table(Gene.color, file="Gene_color1.txt", sep="\t", row.names=F) } Genecolor(Gene) Tae-Jin Researcher in NC State University [[alternative HTML version deleted]]
Sorry. I made a typo. It is the 5th column (not 4th). Thank you. Tae-Jin Begin forwarded message:> From: Tae-Jin Lee <tjlee@ncsu.edu> > Date: February 4, 2011 7:09:38 PM EST > To: r-help@R-project.org > Subject: Help!!! from R beginner > > Hello, > > I'm trying to add a column to the following data frame. The new > column will contain "black" when the 5th column(if_TE_related) is > "TE_related", or "orange" when the 4th column is " " (space). > > "chromo" "MSU_locus" "end5" "end3" "if_TE_related" > "chr04" "LOC_Os04g01006" 1032 2679 "TE_related" > "chr04" "LOC_Os04g01008" 7636 3951 "TE_related" > "chr04" "LOC_Os04g01010" 9521 10296 "TE_related" > "chr04" "LOC_Os04g01020" 17165 17437 " " > "chr04" "LOC_Os04g01030" 29372 18440 "TE_related" > "chr04" "LOC_Os04g01040" 30637 37300 "TE_related" > ... > > So, after a data manipulation, it should look like the following... > > "chromo" "MSU_locus" "end5" "end3" "if_TE_related" "color" > "chr04" "LOC_Os04g01006" 1032 2679 "TE_related" "black" > "chr04" "LOC_Os04g01008" 7636 3951 "TE_related" "black" > "chr04" "LOC_Os04g01010" 9521 10296 "TE_related" "black" > "chr04" "LOC_Os04g01020" 17165 17437 " " "orange" > "chr04" "LOC_Os04g01030" 29372 18440 "TE_related" "black" > "chr04" "LOC_Os04g01040" 30637 37300 "TE_related" "black" > ... > > I have worked on the following code to do this job using function > and loop, but it is not working. If someone help me, I would really > appreciate!!! > The original data frame is Os.chr4.gene.new. > > Gene <- Os.chr4.gene.new[, c("if_TE_related")] > Genecolor <- function(Gene) { > lg <-length(Gene) > for(i in 1:lg) { > if (Gene == "TE_related") {D1 <- (Gene == "black")} > if (Gene == " ") {D1 <- (Gene == "orange")} > } > Gene.color <- cbind(Gene, D1) > write.table(Gene.color, file="Gene_color1.txt", sep="\t", > row.names=F) > } > Genecolor(Gene) > > > Tae-Jin > Researcher in NC State University > > > >[[alternative HTML version deleted]]
You should be able to use 'ifelse' Os.chr4.gene.new$color <- ifelse(Os.chr4.gene.new$if_TE_related == "TE_related", "black", "orange") On Fri, Feb 4, 2011 at 7:09 PM, Tae-Jin Lee <tjlee at ncsu.edu> wrote:> Hello, > > I'm trying to add a column to the following data frame. The new column > will contain "black" when the 5th column(if_TE_related) is > "TE_related", or "orange" when the 4th column is " " (space). > > "chromo" ? ? ? ?"MSU_locus" ? ? "end5" ?"end3" ?"if_TE_related" > "chr04" "LOC_Os04g01006" ? ? ? ?1032 ? ?2679 ? ?"TE_related" > "chr04" "LOC_Os04g01008" ? ? ? ?7636 ? ?3951 ? ?"TE_related" > "chr04" "LOC_Os04g01010" ? ? ? ?9521 ? ?10296 ? "TE_related" > "chr04" "LOC_Os04g01020" ? ? ? ?17165 ? 17437 ? " " > "chr04" "LOC_Os04g01030" ? ? ? ?29372 ? 18440 ? "TE_related" > "chr04" "LOC_Os04g01040" ? ? ? ?30637 ? 37300 ? "TE_related" > ... > > So, after a data manipulation, it should look like the following... > > "chromo" ? ? ? ?"MSU_locus" ? ? "end5" ?"end3" ?"if_TE_related" "color" > "chr04" "LOC_Os04g01006" ? ? ? ?1032 ? ?2679 ? ?"TE_related" ? ?"black" > "chr04" "LOC_Os04g01008" ? ? ? ?7636 ? ?3951 ? ?"TE_related" ? ?"black" > "chr04" "LOC_Os04g01010" ? ? ? ?9521 ? ?10296 ? "TE_related" ? ?"black" > "chr04" "LOC_Os04g01020" ? ? ? ?17165 ? 17437 ? " " ? ? "orange" > "chr04" "LOC_Os04g01030" ? ? ? ?29372 ? 18440 ? "TE_related" ? ?"black" > "chr04" "LOC_Os04g01040" ? ? ? ?30637 ? 37300 ? "TE_related" ? ?"black" > ... > > I have worked on the following code to do this job using function and > loop, but it is not working. If someone help me, I would really > appreciate!!! > The original data frame is Os.chr4.gene.new. > > Gene <- Os.chr4.gene.new[, c("if_TE_related")] > Genecolor <- function(Gene) { > ? ? ? ?lg <-length(Gene) > ? ? ? ?for(i in 1:lg) { > ? ? ? ?if (Gene == "TE_related") {D1 <- (Gene == "black")} > ? ? ? ?if (Gene == " ") {D1 <- (Gene == "orange")} > ? ? ? ?} > ? ? ? ?Gene.color <- cbind(Gene, D1) > ? ? ? ?write.table(Gene.color, file="Gene_color1.txt", sep="\t", row.names=F) > ? ? ? ?} > Genecolor(Gene) > > > Tae-Jin > Researcher in NC State University > > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?