Damo H
Damo H

Reputation: 77

Getting a mean from a portion of a column in a data frame

I've got problems with trying to find a mean of a section of a column in a data frame. The first thing I have run into is that the mean as to be a specific part of the data in the column. I do not know how to get R to do this.

The second thing I need to set a marker in the column, as i then need to subtract the mean from the rest of the data, but the marker changes based on what data.frame I'm looking at.

Ive linked the data file. the section that I wanted to find the mean of is from sample 1-168, before the number resets back to 1. When the number resets is the point i need to start subtracting the mean from

Thanks!

Upvotes: 0

Views: 946

Answers (1)

TarJae
TarJae

Reputation: 78927

Ok. This is a try for the Tempcolumn:

  1. Load your data
# load data
library(readr)
# Data 1.txt is in project folder
Data_1 <- read_csv("./Data 1.txt", col_names = FALSE) 
View(Data_1)
  1. Tweak data
#assign row 5 as colnames
colnames(Data_1) <- Data_1[5,] 

# remove row 1:5 
Data_1 <- Data_1[c(-1:-5),] 
View(Data_1)
  1. Split dataframe Data_1 in df1 rows 1:163 and calculate the mean of Temp = -1673.325
df1 <- Data_1 %>% 
  slice(1:163) %>% 
  mutate(Temp = as.numeric(Temp), na.rm=TRUE) %>%
  summarise(Mean_Temp = mean(Temp), na.rm=TRUE)
  1. Substract Mean_Temp from Temp in df2 rows 164:n
df2 <- Data_1 %>% 
  slice(-1:-164) %>% 
  mutate(Temp = as.numeric(Temp)) %>% 
  mutate(Diff_meanTemp = Temp - df1$Mean_Temp, na.rm=TRUE)

Upvotes: 1

Related Questions