[R] Help with making Loop
Mark Sharp
msharp at txbiomed.org
Sun May 3 04:12:15 CEST 2015
Fazal,
I am not sure what you want, but I have guessed. I have tried to provide a straight forward simplistic solution.
If you examine the intermediate results, I think what is being done will be clear.
Mark
Michael Dewey’s suggestion to look at merge is excellent. You may also need to look at the other functions used below. All are commonly used.
# Code follows
set.seed(1) # ensures you get the same results
id <- paste0("id_", 1:10) # I did not want to copy down your Ids so I made them up.
small_dataframe <- data.frame(unique_sample_id = id,
g_1 = character(length(id)),
g_2 = character(length(id)))
# Making up some genotypes for g_1_table and g_2_table
g_1_table <-
data.frame(unique_sample_id = sample(id, length(id), replace = FALSE),
cond = sample(c("M", "N/M"), length(id), replace = TRUE,
prob = c(0.5, 0.5)))
g_2_table <-
data.frame(unique_sample_id = sample(id, length(id), replace = FALSE),
cond = sample(c("M", "N/M"), length(id), replace = TRUE,
prob = c(0.5, 0.5)))
new_dataframe <- merge(small_dataframe, g_1_table, by = "unique_sample_id")
names(new_dataframe) <-
c("unique_sample_id", "g_1", "g_2", "g_1_cond")
new_dataframe <- merge(new_dataframe, g_2_table, by = "unique_sample_id")
names(new_dataframe) <-
c("unique_sample_id", "g_1", "g_2", "g_1_cond",
"g_2_cond")
new_dataframe$g_1_emoticon <- ifelse(new_dataframe$g_1_cond == "M",
":-)", "No")
new_dataframe$g_2_emoticon <- ifelse(new_dataframe$g_2_cond == "M",
":-)", "No")
new_dataframe
# End of code
# Output of last line of code.
unique_sample_id g_1 g_2 g_1_cond g_2_cond g_1_emoticon g_2_emoticon
1 id_1 M N/M :-) No
2 id_10 N/M N/M No No
3 id_2 M M :-) :-)
4 id_3 N/M M No :-)
5 id_4 N/M N/M No No
6 id_5 M N/M :-) No
7 id_6 M N/M :-) No
8 id_7 N/M M No :-)
9 id_8 N/M M No :-)
10 id_9 M M :-) :-)
> On May 1, 2015, at 3:05 PM, Hadi Fazal <fazal.hadi at curie.fr> wrote:
>
> Hi everyone,
> I am a real beginner to R and have probably a very naive issue. I've a small data frame with three columns: Unique Sample ID, Gene 1 and Gene 2 (the columns on Gene1 and Gene2 are empty). I have two separate tables for the genes which contain the Unique Subject ID in one column and information on whether the gene is mutated or not in that particular subject (M, N/M) in another column called (Condition). I want to make a loop which can read the Unique Subject ID from my data frame, then look up for the same ID in the two tables and depending on whether the gene is mutated (M)/not mutated (N/M), inserts Yes like emoticon / No (N) in the appropriate gene column (Gene1/Gene2) for each Subject ID.
> If anyone can help, I would really appreciate
> Thanks in advance
>
> Fazal,
> <Gene2.png>______________________________________________
> 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