[R] Filling dataframe incorrectly in for loop

petermec petermec at buffalo.edu
Tue Nov 6 12:50:14 CET 2012


Hi everyone,

I am writing a simple script to read in a data file, search through the data
file for an exact character match for a microRNA name, subset those rows,
and aggregrate a dataframe with those subselections all in a for loop. This
is what I have so far:

#READ IN DATA
file1 = "C:/Desktop/mirtarget2_predictions.txt"
data = read.table(file1, header=FALSE, sep="\t")
colnames(data) = c("mir", "gene", "score")
data$mir = as.character(data$mir)
data$gene = as.character(data$gene)

#input miR of Interest
mirs = c("hsa-miR-146a-5p", "hsa-miR-215")

#create table of miR targets
table = data.frame(data[0,])
for(i in 1:length(mirs))
{
holder = subset(data, data$mir==mirs[i])
table = holder
}

The problem is that each time the loop runs, table is being overwritten by
holder, and I can't create something like
table[i,] = holder because length of i is equal to 2. Probably an easy
solution but I am overthinking it at this point.

Thanks in advance.



--
View this message in context: http://r.789695.n4.nabble.com/Filling-dataframe-incorrectly-in-for-loop-tp4648545.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list