I am trying to count how many 'S' appears in each column as 'downstream' from 1 to 10 rows and then as 'upstream' from 15 to 25.
Then I want to save the output in a text file. Well, I managed to work out for a single example. Unfortunately, I have a problem with a loop through the columns to be saved as well. In this case, the number of columns is 5 but that can vary depending on the file.
#data frame
S <- data.frame(scale = c(0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0),
aa = c('A','C','D','E','F','G','H','I','K','L','M','N','P','Q','R','S','T','V','W','Y'))
#input (example)
V1 V2 V3 V4 V5
1 C D E R N
2 C A M K P
3 V T Q Q E
4 A T S S S
5 C D E R N
6 C A M K P
7 V T Q Q E
8 A T S S S
9 R V D S A
10 W R H I C
11 S N I P T
12 Q A S D E
13 C D E R N
14 C A M K P
15 V T Q Q E
16 A T S S S
17 C D E R N
18 C A M K P
19 V T Q Q E
20 A T S S S
21 R V D S A
22 W R H I C
23 S N I P T
24 G A D S S
25 N T T S A
# matching the data from two data frames
df11 <- df_trial %>%
pivot_longer(cols = everything(), values_to = 'aa') %>%
mutate(aa = replace(aa, aa == '-', '')) %>%
left_join(S, by = 'aa') %>%
arrange(name) %>%
group_by(name) %>%
mutate(row = row_number())
view(df11)
values_for_all <- df11 %>%
pivot_wider(names_from = name, values_from = c(scale, aa)) %>%
select(-row)
view(values_for_all)
#class(values_for_all)
#problem with loop through the columns here:!!!!!!!!!!!!!!!!!
#sum values from positions 1 to 10 and then from 15 to 25
downstream <- sum(values_for_all$scale_V1[1:11])
#view(downstream)
upstream <- sum(values_for_all$scale_V1[15:25])
#view(upstream)
res <- cbind(downstream,upstream)
res_trial<- as.data.frame(t(res))
view(res_trial)
#class(res_trial)
#converting a matrix to the data frame
res_final <- as.data.frame(t(res_trial))
view(res_final)
#class(res_final)
#saving to a text file
write.table(res_final,"~/Desktop/R_work/test.txt",sep="\t",row.names=FALSE)
#expected outcome (example):
downstream upstream
2 0
0 0
Thank you for your help!