![](/img/trans.png)
[英]How to write an R function to create a new column in any dataframe from conditional values of two existing columns?
[英]Create a new column by match conditional in two columns in pairwise dataframe in R
我正在嘗試通過匹配具有相同因素的兩列的條件來創建一個新列,以匯總數據。
這就是 dataframe 的外觀
這就是我想總結的方式
這是我建議的不起作用的代碼:
probe$Cluster<-"cluster"
for (i in 1:length(probe)){
if(is.na(probe[i,4])){
probe[i,9]<-probe[i,6]}
if(is.na(probe[i,6])){
probe[i,9]<-probe[i,4]}
if(identical(probe[i,4],probe[i,6])){
probe[i,9]<-probe[i,4]}}
if(!identical(probe[i,4],probe[i,6])){
probe[i,9]<-probe[i,4]
rep(probe[i,1:9]%>%probe[i,9]<-probe[i,6}
#Then create a summary of this like this:
Sum<-probe%>%group_by(Method,Cluster)%>% summarise(mean(relation, na.rm
=FALSE),numberobservations=length(unique(GenA)))%>%data.frame()
謝謝你的任何建議
如果沒有我可以加載的示例數據(無需從圖片重新輸入),則無法驗證,但看起來您正在尋找這樣的東西:
library(dplyr)
probe %>%
mutate(Cluster = coalesce(ClusterA, ClusterB) %>% # use 1st non-NA from cols
group_by(Method, Cluster) %>%
summarize(mean = mean(relation, na.rm = TRUE),
numberobservations = n(), .groups = "drop")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.