我试图计算每列中有多少“S”出现在从 1 到 10 行的“下游”,然后作为从 15 到 25 的“上游”。
然后我想将输出保存在文本文件中。好吧,我设法解决了一个例子。不幸的是,我也有一个循环遍历要保存的列的问题。在这种情况下,列数为 5,但可能因文件而异。
#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 循环遍历此处的列:!!!!!!!!!!!!!!!!!!
#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
感谢您的帮助!