2014-10-09 104 views
0

我想計算一個列的元素的平均值直到NA值,然後重新開始計算其餘元素的平均值並將所有平均值放入列表中。 我的數據是這樣的(取塔V4的平均投入列表的均值:計算平均值直到達到NA值,然後繼續R

     V1  V2  V3 V4 
1     chr1 3686375 3686400 6 
2     chr1 3686400 3686425 8 
3 Next bedGraph section  NA  NA NA 
4     chr1 3840175 3840200 2 
5     chr1 3840200 3840225 3 
6     chr1 3840225 3840250 4 

的列表的均值= (7,3)

+0

請添加一個'dput (頭(...))'的數據。 – Alex 2014-10-09 05:29:23

回答

4

你的數據結構,使得它很難做這樣的計算創建一個bedgraph變量,其作用是識別每個單獨bedGraph部分,然後刪除所有這些「分」行從設定數據。然後,你可以使用aggregate計算mean每個部分。

# Read in the data 
data=read.table(text='V1 V2 V3 V4 chr1 3686375 3686400 6 chr1 3686400 3686425 8 "Next bedGraph section" NA  NA NA chr1 3840175 3840200 2 chr1 3840200 3840225 3 chr1 3840225 3840250 4 ',head=TRUE) 

# Make a bedgraph variable. 
data$bedgraph <- cumsum(data$V1 == 'Next bedGraph section') 
data <- data[data$V1!='Next bedGraph section', ] 

# Alternatively, using the NA 
data$bedgraph <- cumsum(is.na(data$V2)) 
data <- data[!is.na(data$V2), ] 

# Find the mean for each bedgraph section 
aggregate(V4~bedgraph, data, mean) 
# bedgraph V4 
# 1  0 7 
# 2  1 3 
+0

感謝您的回覆。這裏的問題是文字'Next bedGraph section'不是一個修復文本。我的實際數據有不同的bedgraph名稱,但它們都在列V2,V3,V4中具有NA值。 – Cina 2014-10-09 09:18:32

+0

請注意,我在'#中爲這種情況提供了合適的答案,或者使用代碼的NA'部分。您可以使用'cumsum(is.na(data $ V2)&is.na(data $ V3)&is.na(data $ V4))'''輕鬆擴展代碼以檢查所有三列是否爲'NA'。 – nograpes 2014-10-09 12:31:43

0

如果您只有一行NA,只需找到行號,然後將您的數據分爲兩個數據幀。

否則,如果多行包含NA,你可以使用包dplyr

library(dplyr) 


index_of_na <- which(is.na(data$V2)) # find rows which contain na 

number_in_each_block <- index_of_na - lag(index_of_na,1) # find number of rows in each block, including the terminating na 
number_in_each_block[[1]] <- index_of_na[[1]] # set the size of first block to the first entry in index_of_na 
number_in_each_block[[length(number_in_each_block) + 1]] <- nrow(data) - index_of_na[[length(index_of_na)]] # count the last block if it is not terminated by na 


list_of_groups_in_data <- paste0("group_", seq_along(number_in_each_block)) # call the groups group_1, group_2, etc... 

group_name <- rep(list_of_groups_in_data, number_in_each_block) # make a vector with the same number of rows as the data 

data <- cbind(data, group_name) # now we have named each row with a group name. 


#then use dplyr group_by to calculate the mean of each group 
data <- 
    data %>% 
    group_by(group_name) %>% 
    mutate(mean_of_groups = mean(V4, na.rm = TRUE)) 

使用

data=read.table(text='V1 V2 V3 V4 \n chr1 3686375 3686400 6 \n chr1 3686400 3686425 8 \n NextbedGraphsection NA  NA NA \n chr1 3840175 3840200 2 \n chr1 3840200 3840225 3 \n chr1 3840225 3840250 4',head=TRUE, sep="") 

我們得到:

> print(data) 
Source: local data frame [6 x 6] 
Groups: group_name 

        V1  V2  V3 V4 group_name mean_of_groups 
1    chr1 3686375 3686400 6 group_1    7 
2    chr1 3686400 3686425 8 group_1    7 
3 NextbedGraphsection  NA  NA NA group_1    7 
4    chr1 3840175 3840200 2 group_2    3 
5    chr1 3840200 3840225 3 group_2    3 
6    chr1 3840225 3840250 4 group_2    3 

你能告訴我怎麼把專欄放在我的頭上an_of_groups'到一個新的矩陣重複元素排除

使用dplyr::summarise代替mutate在代碼的最後一塊:

data <- 
    data %>% 
    group_by(group_name) %>% 
    summarise(mean_of_groups = mean(V4, na.rm = TRUE)) 

這給:

> data 
Source: local data frame [2 x 2] 

    group_name mean_of_groups 
1 group_1    7 
2 group_2    3 
+0

謝謝@亞歷克斯,這工作。你能告訴我如何將列'mean_of_groups'排除在重複元素之外的新矩陣中嗎? – Cina 2014-10-09 09:30:06