# standardized value

Hi, I want to calculate standardized value for temp for every 800 to 1300 (Year_CE) cycle. Please can anyone help me?
My code is here--

stand_6 <- data  %>%
group_by(Long, Lat, Zone, floor(Years_CE/5)*5) %>%
summarize(temp = scale(temp))


My data looks like this--

structure(list(Long = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Lat = c(35.0526315789474,
35.0526315789474, 35.0526315789474, 35.0526315789474, 35.0526315789474,
35.0526315789474, 36.9473684210526, 36.9473684210526, 36.9473684210526,
36.9473684210526, 36.9473684210526, 36.9473684210526, 38.8421052631579,
38.8421052631579, 38.8421052631579, 38.8421052631579, 38.8421052631579,
38.8421052631579, 40.7368421052632, 40.7368421052632, 40.7368421052632,
40.7368421052632, 40.7368421052632, 40.7368421052632, 42.6315789473684,
42.6315789473684, 42.6315789473684, 42.6315789473684, 42.6315789473684,
42.6315789473684), Years_CE = c(800, 900, 1000, 1100, 1200, 1300,
800, 900, 1000, 1100, 1200, 1300, 800, 900, 1000, 1100, 1200,
1300, 800, 900, 1000, 1100, 1200, 1300, 800, 900, 1000, 1100,
1200, 1300), temp = c(15.0901016786071, 15.0450387807642, 15.0348324884123,
15.0064519669855, 14.9790039334339, 14.9957665121179, 19.0163904543675,
18.9728754517259, 18.976677467672, 18.9414334730997, 18.9061382287457,
18.9246854815801, 18.2527539480474, 18.2095333045452, 18.2200694920057,
18.1803239596273, 18.1434029589801, 18.1606602884125, 16.8848757284685,
16.8548353656338, 16.8748138471184, 16.8255589520872, 16.7788528685565,
16.7824846105795, 9.71015114194835, 9.69266028524531, 9.7210116755362,
9.66587749072316, 9.60536010470156, 9.60387509871257), Zone = c("Mid-Latitude",
"Mid-Latitude", "Mid-Latitude", "Mid-Latitude", "Mid-Latitude",
"Mid-Latitude", "Mid-Latitude", "Mid-Latitude", "Mid-Latitude",
"Mid-Latitude", "Mid-Latitude", "Mid-Latitude", "Mid-Latitude",
"Mid-Latitude", "Mid-Latitude", "Mid-Latitude", "Mid-Latitude",
"Mid-Latitude", "Mid-Latitude", "Mid-Latitude", "Mid-Latitude",
"Mid-Latitude", "Mid-Latitude", "Mid-Latitude", "Mid-Latitude",
"Mid-Latitude", "Mid-Latitude", "Mid-Latitude", "Mid-Latitude",
"Mid-Latitude")), row.names = 397:426, class = "data.frame")


Assuming by standardized you mean you want to calculate X_{standardized} = \frac{X - \mu}{\sigma}, meaning that the mean will be centered on 0 with a standard deviation of 1, then you want to use mutate():

stand_6 <- data  %>%
group_by(Long, Lat, Zone, floor(Years_CE/5)*5) %>%
mutate(temp_std = (temp - mean(temp))/sd(temp)


Hi, thank you so much for response. It is creating NA values. Please, check this here--

structure(list(X = 35533:35542, Long = c(-25, -25, -25, -25,
-25, -25, -25, -25, -25, -25), Lat = c(35.0526315789474, 35.0526315789474,
35.0526315789474, 35.0526315789474, 35.0526315789474, 35.0526315789474,
36.9473684210526, 36.9473684210526, 36.9473684210526, 36.9473684210526
), Years_CE = c(800L, 900L, 1000L, 1100L, 1200L, 1300L, 800L,
900L, 1000L, 1100L), temp = c(19.7816231064427, 19.743373226854,
19.7296853842946, 19.7233643480971, 19.7181683132701, 19.7269002134387,
19.0137390529199, 18.9855507014916, 18.9726161520249, 18.9673467852444
), Zone = c("Mid-Latitude", "Mid-Latitude", "Mid-Latitude", "Mid-Latitude",
"Mid-Latitude", "Mid-Latitude", "Mid-Latitude", "Mid-Latitude",
"Mid-Latitude", "Mid-Latitude"), floor(Years_CE/5) * 5 = c(800,
900, 1000, 1100, 1200, 1300, 800, 900, 1000, 1100), temp_std = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_)), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -10L), groups = structure(list(
Long = c(-25, -25, -25, -25, -25, -25, -25, -25, -25, -25
), Lat = c(35.0526315789474, 35.0526315789474, 35.0526315789474,
35.0526315789474, 35.0526315789474, 35.0526315789474, 36.9473684210526,
36.9473684210526, 36.9473684210526, 36.9473684210526), Zone = c("Mid-Latitude",
"Mid-Latitude", "Mid-Latitude", "Mid-Latitude", "Mid-Latitude",
"Mid-Latitude", "Mid-Latitude", "Mid-Latitude", "Mid-Latitude",
"Mid-Latitude"), floor(Years_CE/5) * 5 = c(800, 900, 1000,
1100, 1200, 1300, 800, 900, 1000, 1100), .rows = structure(list(
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L), .drop = TRUE))


This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.