Transform dataframe in R using dplyr - r

I have a dataframe df
df<- structure(list(year = structure(c(72L, 2L, 13L, 24L, 35L, 46L,
57L, 68L, 70L, 71L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 25L, 26L, 27L,
28L, 29L, 30L, 31L, 32L, 33L, 34L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L,
56L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 69L), .Label = c("day",
"id.1.00000", "id.10.0000", "id.11.0000", "id.12.0000", "id.13.0000",
"id.14.0000", "id.15.0000", "id.16.0000", "id.17.0000", "id.18.0000",
"id.19.0000", "id.2.00000", "id.20.0000", "id.21.0000", "id.22.0000",
"id.23.0000", "id.24.0000", "id.25.0000", "id.26.0000", "id.27.0000",
"id.28.0000", "id.29.0000", "id.3.00000", "id.30.0000", "id.31.0000",
"id.32.0000", "id.33.0000", "id.34.0000", "id.35.0000", "id.36.0000",
"id.37.0000", "id.38.0000", "id.39.0000", "id.4.00000", "id.40.0000",
"id.41.0000", "id.42.0000", "id.43.0000", "id.44.0000", "id.45.0000",
"id.46.0000", "id.47.0000", "id.48.0000", "id.49.0000", "id.5.00000",
"id.50.0000", "id.51.0000", "id.52.0000", "id.53.0000", "id.54.0000",
"id.55.0000", "id.56.0000", "id.57.0000", "id.58.0000", "id.59.0000",
"id.6.00000", "id.60.0000", "id.61.0000", "id.62.0000", "id.63.0000",
"id.64.0000", "id.65.0000", "id.66.0000", "id.67.0000", "id.68.0000",
"id.69.0000", "id.7.00000", "id.70.0000", "id.8.00000", "id.9.00000",
"month"), class = "factor"), `2012` = c(1, 76.1, 38.1, 88.6,
132.5, 241.9, 343.2, 343.2, 291.7, 82.8, 17.6, 17.6, 17.5, 17.6,
17.6, 17.6, 17.5, 18.7, 20.4, 17.3, 16.8, 67.6, 66.2, 16.8, 16.8,
16.8, 121.8, 0.1, 99.9, 101.5, 52, 81.6, 68.5, 37.6, 46.2, 23.4,
31, 55.7, 24, 384.8, 53.3, 22.7, 18.6, 25.4, 42.9, 16.5, 75,
69.7, 43.2, 90.9, 18.5, 53.1, 21.6, 66.9, 40.8, 153.3, 47.2,
98.6, 75.8, 141.6, 9.8, 12.7, 12.7, 69.2, 18.3, 359, 153.4, 24.3,
24.3, 24.3, 7.9), `2012` = c(2, 104.6, 41.4, 24.8, 25, 299.3,
181.6, 181.6, 223, 49.1, 9.3, 9.3, 8.7, 9.3, 9.3, 9.3, 8.7, 9.6,
5.2, 3.9, 4.4, 29.2, 30.3, 4.4, 4.4, 4.4, 49.1, 7.2, 27.7, 12.7,
20.5, 15.2, 19.7, 4.8, 35.7, 31.6, 6, 7.4, 2.1, 452.5, 43.2,
92.8, 62, 17.3, 8.4, 14.2, 94.5, 60.1, 28.3, 18.8, 0, 49.1, 11.4,
47.7, 44.6, 73.3, 23, 62.9, 30.5, 95, 45.8, 42.4, 42.4, 31.1,
33.8, 340, 63.8, 12.9, 12.9, 12.9, 8.3), `2012` = c(3, 196.1,
75.8, 26.7, 27.5, 410.1, 287.8, 287.8, 254.9, 56.9, 24.6, 24.6,
20.8, 24.6, 24.6, 24.6, 20.8, 13.3, 21.3, 20.7, 22.9, 41.2, 42.9,
22.9, 22.9, 22.9, 63.4, 34.1, 15.6, 11.1, 8.5, 10.4, 8.1, 35.6,
27, 20.2, 18.1, 33.7, 45.3, 363.2, 32.3, 28.9, 14.6, 10.7, 45.6,
18.3, 159.2, 51.7, 33.6, 17.5, 52.8, 62.9, 20.5, 28.3, 21, 28.1,
24.3, 48.1, 41.6, 179.6, 2.7, 36.9, 36.9, 59, 41.4, 349, 59.2,
21.5, 21.5, 21.5, 31.7), `2012` = c(4, 27.5, 45.4, 87.6, 123.1,
290.6, 99.9, 99.9, 103.9, 33.7, 24.9, 24.9, 21.2, 24.9, 24.9,
24.9, 21.2, 15.6, 57.2, 48.3, 43.6, 51.8, 54.8, 43.6, 43.6, 43.6,
67.8, 58.6, 26.5, 25.5, 26.6, 19.3, 24.2, 24.8, 43.8, 39.2, 67.4,
190.8, 121.5, 462.7, 0, 79.8, 45.8, 23.6, 161.1, 120.7, 119.8,
44.4, 62, 66.4, 63.1, 68, 19.4, 50.8, 63.7, 122.9, 119.3, 103.1,
81.3, 97.1, 14.8, 36.6, 36.6, 35.3, 82.3, 313, 75.1, 18.6, 18.6,
18.6, 67.1), `2012` = c(5, 72.5, 102.2, 66.2, 70.8, 214.6, 73.4,
73.4, 69.7, 72.1, 35.5, 35.5, 31, 35.5, 35.5, 35.5, 31, 24.1,
67.8, 67, 60.1, 82.3, 86, 60.1, 60.1, 60.1, 48.5, 62.7, 53.4,
54.2, 42.9, 55.6, 19, 0, 47.5, 26.1, 91.5, 32.5, 51.6, 389.3,
0, 30.5, 10.9, 40.4, 89.5, 62.9, 123.9, 71.2, 151.6, 69.4, 101.4,
49.8, 27.1, 35, 19.6, 72.6, 31.7, 120, 143.3, 54.2, 8.8, 3.6,
3.6, 33.3, 143.8, 221, 59.5, 25.3, 25.3, 25.3, 314.6), `2012` = c(6,
101.8, 124.8, 117, 136.3, 194.3, 142.1, 142.1, 135.2, 87.9, 75.2,
75.2, 72.7, 75.2, 75.2, 75.2, 72.7, 61.5, 101.8, 108.6, 103.7,
125.8, 131.9, 103.7, 103.7, 103.7, 52.8, 200.5, 81.9, 103.6,
68.5, 83.2, 65.2, 1.2, 79.6, 64.7, 76.3, 73, 35.6, 238.4, 0,
13.6, 0.1, 26.7, 65, 17.5, 169.5, 36.3, 71.8, 86.4, 3.1, 117.9,
23.6, 61.6, 93.3, 143.1, 122.6, 167, 96.7, 77.6, 33.5, 0, 0,
94.8, 160, 220, 89.3, 23.3, 23.3, 23.3, 399.9), `2012` = c(7,
62.8, 68.9, 108.8, 180.2, 189.2, 51.8, 51.8, 46.2, 50.2, 68.7,
68.7, 64.1, 68.7, 68.7, 68.7, 64.1, 60, 107.8, 101.8, 90.7, 40.6,
41.7, 90.7, 90.7, 90.7, 53.8, 289.2, 72.9, 86.2, 119.4, 66.8,
59.4, 0, 97.5, 67.3, 63.4, 47.7, 33, 142, 0, 35.8, 17.9, 66.1,
25.6, 0.6, 246.8, 109.1, 122.4, 89.7, 2.6, 44.6, 8, 91, 60.2,
100.8, 99.8, 59.2, 112.9, 10.2, 66.5, 0.8, 0.8, 64.5, 131.4,
173, 50.4, 7.3, 7.3, 7.3, 590), `2012` = c(8, 98.2, 81.1, 40.7,
48.9, 61.8, 33.5, 33.5, 27.1, 73.7, 80.9, 80.9, 82.1, 80.9, 80.9,
80.9, 82.1, 82, 75.9, 78.3, 75.5, 99.3, 97.2, 75.5, 75.5, 75.5,
44.2, 240.8, 46.8, 44.7, 51.4, 45.2, 46, 2.6, 43.8, 23.5, 10.6,
21.1, 40.1, 91.6, 0, 23.5, 18.6, 34.8, 48, 36.2, 124.6, 153.4,
146.6, 87.2, 12.4, 98, 60.6, 69.1, 118.4, 115.1, 43.6, 108.1,
117.8, 0, 5.8, 13.9, 13.9, 57.4, 79, 165, 67.5, 60, 60, 60, 357
), `2012` = c(9, 23.9, 38.9, 53.2, 60.9, 44.2, 17.3, 17.3, 17.4,
54.2, 45.6, 45.6, 43.6, 45.6, 45.6, 45.6, 43.6, 44.8, 30.8, 27.2,
27.2, 79.2, 84.3, 27.2, 27.2, 27.2, 95.1, 125.9, 42.7, 30.8,
27.4, 43.1, 35.5, 49.2, 64.4, 22.5, 38.6, 60.6, 67.1, 20.3, 0,
69, 9, 131.8, 56.1, 58.2, 272.8, 163.3, 198.6, 50.9, 67.1, 75.6,
85.3, 72.4, 63.5, 126.9, 63.2, 118.7, 112, 0.2, 32.8, 10.3, 10.3,
48.2, 30.2, 169, 125.8, 87, 87, 87, 512.9), `2012` = c(10, 81.6,
29.2, 112.8, 119.8, 29.1, 330.5, 330.5, 295.4, 111, 60.3, 60.3,
54.9, 60.3, 60.3, 60.3, 54.9, 43.8, 53.6, 63.8, 67.7, 84.4, 87.8,
67.7, 67.7, 67.7, 166.6, 47.3, 38.3, 39.2, 44.2, 33, 46.6, 40.6,
92.5, 60.3, 84.4, 122.7, 184.9, 24.1, 2.1, 48.4, 22.5, 81.8,
111.3, 148.3, 119.7, 120, 142.3, 91.8, 150.6, 108.9, 55.9, 65.6,
69.2, 135.9, 110.7, 114.6, 100, 103.4, 28.2, 38.4, 38.4, 127.2,
87, 203, 172, 53.8, 53.8, 53.8, 258.7), `2012` = c(11, 92.3,
60.8, 41.8, 68.3, 43.6, 284.6, 284.6, 276.4, 45.9, 23.7, 23.7,
21.4, 23.7, 23.7, 23.7, 21.4, 17.1, 27.2, 26.5, 26.5, 33.4, 36.8,
26.5, 26.5, 26.5, 31.8, 43.9, 67.4, 46.6, 69.8, 62.5, 40.3, 68.2,
38.8, 54.6, 39.2, 81.9, 66.7, 73.1, 56.9, 177.4, 194.8, 136.5,
184.8, 183.7, 126.6, 158.5, 200.9, 37.2, 238.5, 84.3, 51.5, 48,
56.3, 103.3, 101, 20.8, 39.4, 161.3, 2.6, 9, 9, 37.6, 33.5, 223,
120.2, 49.6, 49.6, 49.6, 54.7), `2012` = c(12, 34, 36.5, 156.7,
195.4, 132.6, 310.4, 310.4, 271.8, 87.6, 15.3, 15.3, 13.2, 15.3,
15.3, 15.3, 13.2, 10.3, 17.5, 15.8, 14.8, 77.5, 74.5, 14.8, 14.8,
14.8, 122.4, 5.1, 81.1, 74.5, 58.7, 69.1, 55.8, 3.3, 48.1, 33.3,
83.9, 173.5, 30.4, 169.4, 41.7, 86.6, 67.9, 36.3, 58.8, 121.9,
100.7, 163.3, 155.5, 135.4, 255.8, 60, 18.7, 67.9, 57.8, 150.3,
136.2, 109.2, 123.3, 179.5, 35.3, 92.4, 92.4, 71.4, 50.5, 237,
135.4, 16.5, 16.5, 16.5, 1.1)), .Names = c("year", "2012", "2012",
"2012", "2012", "2012", "2012", "2012", "2012", "2012", "2012",
"2012", "2012"), row.names = c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L,
36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L,
49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L,
62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L), class = "data.frame")
I want to transform my dataframe using the R package dplyr. So far, year and month are in rows but I want them in columns. Basically I want a dataframe with a column containing the IDs, one with year, one with month and one with the values.
The structure will look like this with an ID column additional.
df2<-structure(list(YEAR = c(1980L, 1980L, 1980L, 1980L, 1980L, 1980L,
1980L, 1980L, 1980L, 1980L), MONTH = 1:10, PRCP = c(46.3, 20.7,
101.3, 27.2, 67.5, 34.1, 12, 95.4, 17, 31.8)), .Names = c("YEAR",
"MONTH", "PRCP"), row.names = c(NA, 10L), class = "data.frame")
Can someone help me out? Thanks

Here is the code using dplyr / tidyr combination and magrittr (for convenience).
Vectorize(require)(package = c("tidyr", # Data manipulation
"dplyr", # Data manipulation
"magrittr" # Reverse pipe
),
char = TRUE)
dfTr <- df %>%
gather(key = key, value = value, convert = TRUE, -year) %>%
setNames(make.names(names(.), unique = TRUE)) %>%
select(year, key, value)
You can ad the following syntax to obtain one id column:
# Unite is optional as it will generate one ID column
unite(year_month, key, year)
which would produce:
year key value
1 month 2012 1.0
2 id.1.00000 2012 76.1
3 id.2.00000 2012 38.1
With exclusion
Sample approach excluding that month row:
dfTr2 <- df %>%
setNames(make.names(names(.), unique = TRUE)) %>%
filter(year != "month") %>%
gather(key = key, value = value, convert = TRUE, -year) %>%
select(year, key, value) %>%
unite(year_month, key, year) %>%
mutate(year_month = gsub("X","",year_month))
Cleaning month value
It crossed my mind that you may want to use the month figure, a small example below extracts the relevant digit:
library(stringr)
dfTr3 <- df %>%
setNames(make.names(names(.), unique = TRUE)) %>%
filter(year != "month") %>%
gather(key = key, value = value, convert = TRUE, -year) %>%
select(year, key, value) %>%
unite(year_month, key, year) %>%
mutate(year_month = gsub("X","",year_month)) %>%
mutate(month = gsub(".", "", str_extract_all(string = year_month,
pattern = "\\.\\d{1,2}\\."),
fixed = TRUE))
which produces:
> head(dfTr3, n = 2)
year_month value month
1 2012_id.1.00000 76.1 1
2 2012_id.2.00000 38.1 2

Your data is in an odd form; it appears that you have two header rows, which is not something supported in an R data frame. Most likely there was something wrong with the initial import, but I can see what it is you are trying to do.
My solution would be to first fix up the data frame so that it shows what we want, which is for the headers to be the combination of the current headers (year) and the first row (month)
> paste(names(df), df[1,])
[1] "year 72" "2012 1" "2012 2" "2012 3" "2012 4" "2012 5" "2012 6"
[8] "2012 7" "2012 8" "2012 9" "2012 10" "2012 11" "2012 12"
That looks good (except for "year 72", which we'll fix up later), so lets set it:
> names(df) <- paste(names(df), df[1,])
> df %>% head
year 72 2012 1 2012 2 2012 3 2012 4 2012 5 2012 6 2012 7 2012 8 2012 9
1 month 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
3 id.1.00000 76.1 104.6 196.1 27.5 72.5 101.8 62.8 98.2 23.9
4 id.2.00000 38.1 41.4 75.8 45.4 102.2 124.8 68.9 81.1 38.9
Now let's get rid of that first row
> df <- tail(df, -1)
> head(df)
year 72 2012 1 2012 2 2012 3 2012 4 2012 5 2012 6 2012 7 2012 8 2012 9
3 id.1.00000 76.1 104.6 196.1 27.5 72.5 101.8 62.8 98.2 23.9
4 id.2.00000 38.1 41.4 75.8 45.4 102.2 124.8 68.9 81.1 38.9
Now we can use tidyr, a companion package to dplyr for reshaping data frames, to put it into the desired form.
> require(tidyr)
> df %>% gather(date, value, -`year 72`) %>% head
year 72 date value
1 id.1.00000 2012 1 76.1
2 id.2.00000 2012 1 38.1
3 id.3.00000 2012 1 88.6
4 id.4.00000 2012 1 132.5
5 id.5.00000 2012 1 241.9
From here, we can use separate to break the "date" column into "year" and "month" columns.

This should do the trick!
rownames(df) <- seq(length=nrow(df))
colnames(df)[2:13] <- c(1:12)
tmp <- melt(df[2:nrow(df),],id = c('year'))
colnames(tmp) <- c('id','Month','Value')
library(sqldf)
sqldf("Select * from tmp order by id")

Related

Finding the 10 closest values to 100 and the 30 closest ones to 30

I have the variables price (dollars) and size (sq.feet). From my dataset, I want to find the 10 houses that best fit the description "price close to 100 and size close to 30".
I can see that I have to use the abs() function, and find the smallest absolute difference. However, I want both conditions to be true at the same time, I don't know how to do that with abs.
I thought about doing
which(abs(...))
but I don't know how.
Any ideas?
The data:
data_struct <-
structure(
list(
new_baltimore.SQFT = c(
11.25,
28.92,
30.62,
26.12,
22.04,
39.42,
21.88,
25.6,
44.12,
19.88,
12.08,
10.99,
12.8,
29.79,
14.3,
13.72,
11.84,
18.06,
10.72,
8.96,
14.38,
36.75,
20,
22.82,
24.86,
19.2,
11.58,
26,
14.4,
11.62,
23.08,
23.76,
15.6,
10,
22.8,
16.76,
22.1,
14.28,
15.36,
16,
23.04,
24.94,
11.82,
12.88,
11.2,
18.12,
38.25,
17.68,
19.02,
32.8,
15.16,
21.975,
12.6,
23.52,
17.52,
47.61,
20.55,
35.52,
8.4,
13.68,
14.48,
12.8,
12.8,
18,
15.4,
10.08,
8.96,
8.96,
20,
12.88,
12,
18.16,
14.28,
26,
12.02,
20.8,
11.78,
8.68,
17.6,
11.4,
44.55,
46.32,
10.24,
9.6,
31.2,
26.4,
13.6,
27.48,
17.86,
18.04,
14.84,
10.46,
14.56,
6.96,
9.5,
11.86,
12.88,
12.32,
6.72,
10.08,
15.6,
6.72,
11.52,
11.76,
10.24,
11.52,
9.28,
6.72,
15.6,
15.5,
9.84,
15.6,
13.76,
10.24,
5.76,
10.08,
11.52,
12.15,
9.77,
15,
14.4,
14.5,
22.54,
10.24,
7.8,
8.4,
10.92,
42.9,
9,
10.5,
10.08,
12.6,
8.96,
8.58,
7.56,
10.8,
13.44,
10.24,
14.44,
12.24,
13.2,
9.6,
15.22,
24.16,
10.24,
10.24,
9.88,
23.2,
17.68,
24.3,
35.94,
21.6,
11.02,
21,
23.92,
14.4,
28,
11.44,
21.94,
10.24,
16.86,
9.92,
13.44,
12,
14.76,
8.96,
11.52,
8.64,
8.12,
11.12,
11.28,
10.36,
11.52,
17.1,
17.52,
10.73,
11.2,
12.8,
12,
41.07,
12.8,
22.36,
10.56,
13.44,
11.02,
17.98,
18.88,
11.76,
9.36,
11.52,
27.3,
23.04,
17.68,
13.36,
11.6,
11.52,
9.98,
12.96,
11.13,
19.6,
11.52,
12.16,
0,
10.64
),
new_baltimore.PRICE = c(
47,
113,
165,
104.3,
62.5,
70,
127.5,
64.5,
145,
63.5,
58.9,
65,
48,
3.5,
12.8,
17.5,
36,
41.9,
53.5,
24.5,
24.5,
55.5,
60,
51,
46,
46,
44,
54.9,
42.5,
44,
44.9,
37.9,
33,
43.9,
49.6,
52,
37.5,
50,
35.9,
42.9,
107,
112,
44.9,
55,
102,
35.5,
62.9,
39,
110,
8,
62,
85.9,
57,
110,
67.7,
89.5,
70,
74,
13,
48,
24,
53.5,
34.5,
53,
87.5,
33.5,
24,
9.6,
30,
41,
30,
38.9,
20.7,
49.9,
18.6,
39,
34,
16,
18.9,
15.2,
41.5,
53,
22,
24.9,
6.7,
32.5,
30,
59,
29.5,
26,
16.5,
39,
48.9,
33.5,
46,
54,
57.9,
37.9,
32,
31,
34,
29,
32.5,
51.9,
31,
41.8,
48,
28,
35,
46.5,
51.9,
35.4,
16,
35,
35,
36.5,
35.9,
45,
40,
35,
38,
37,
23,
25.5,
39.5,
21.5,
9,
67.5,
13.4,
12.5,
28.5,
23,
33.5,
9,
11,
30.9,
31.65,
33,
33.4,
47,
40,
46,
45.5,
57,
29.9,
30,
34,
51,
64.5,
57.5,
85.5,
61,
38,
56.5,
60.4,
51.5,
54,
69,
56,
27.9,
37.5,
32.9,
22,
29.9,
39.9,
32.6,
38.5,
21.5,
25.9,
27.5,
22.9,
31.5,
8.5,
5.5,
33,
57,
47,
43.5,
43.9,
68.5,
44.25,
61,
40,
44.5,
57,
35,
35.1,
64.5,
40,
42.6,
50,
58,
58,
55,
43,
54,
39,
45,
42,
38.9,
43.215,
26.5,
30,
29.5
)
),
class = "data.frame",
row.names = c(NA,-204L)
)
I think you're right with absolute differences. Try to subtract price and size to get a zero point, similar to de-meaning. Using replicate we subtract a respective matrix. From the absolute differences first rank the columns then again the rowSums. The first k values of the order should give you the desired appartements!
f <- \(price, size, k, data) {
aux <- abs(data - t(replicate(nrow(data), c(size, price))))
data[order(rank(rowSums(as.data.frame(lapply(aux, rank)))))[1:k], ]
}
f(price=100, size=30, k=10, data=data_struct[1:2])
# new_baltimore.SQFT new_baltimore.PRICE
# 2 28.920 113.0
# 4 26.120 104.3
# 42 24.940 112.0
# 54 23.520 110.0
# 58 35.520 74.0
# 41 23.040 107.0
# 151 35.940 85.5
# 8 25.600 64.5
# 88 27.480 59.0
# 52 21.975 85.9
You could do:
data_struct[head(order(pdist::pdist(data_struct, c(30,100))#dist),10), ]
new_baltimore.SQFT new_baltimore.PRICE
4 26.120 104.3
41 23.040 107.0
54 23.520 110.0
42 24.940 112.0
2 28.920 113.0
49 19.020 110.0
151 35.940 85.5
52 21.975 85.9
45 11.200 102.0
65 15.400 87.5
using tidyverse you could do:
data_struct %>%
mutate(price_diff = (new_baltimore.PRICE -100)^2,
size_diff = (new_baltimore.SQFT-30)^2) %>%
slice_min(price_diff + size_diff, n=10) %>%
select(-ends_with('diff'))
new_baltimore.SQFT new_baltimore.PRICE
1 26.120 104.3
2 23.040 107.0
3 23.520 110.0
4 24.940 112.0
5 28.920 113.0
6 19.020 110.0
7 35.940 85.5
8 21.975 85.9
9 11.200 102.0
10 15.400 87.5
in base R:
data_struct[head(order(colSums((t(data_struct) - c(30, 100))^2)), 10),]
new_baltimore.SQFT new_baltimore.PRICE
4 26.120 104.3
41 23.040 107.0
54 23.520 110.0
42 24.940 112.0
2 28.920 113.0
49 19.020 110.0
151 35.940 85.5
52 21.975 85.9
45 11.200 102.0
65 15.400 87.5

Converting two couple 2 columns into a long format dataset: how to do?

Locked for 351 days. There are disputes about this question’s content being resolved at this time. It is not currently accepting new answers or interactions.
I have tried to manipulate the original dataset I had as follows.
db%>%
mutate(GROUP = fct_recode(GROUP, CONTROL = "CONTROLLO")) %>%
to_long(keys = c("tests0", "tests7"),
values =c("score0", "score7"),
grep("T0$", names(.), value = TRUE),
grep("T7$", names(.), value = TRUE)) %>%
as_tibble %>%
mutate(Gender = factor(Gender, levels = c(1, 0), labels = c("M", "F")),
across(c(tests0, tests7), factor))
And I have obtained the following dataset
# A tibble: 678 x 9
ID GROUP Gender Age Education tests0 score0 tests7 score7
<dbl> <fct> <fct> <dbl> <dbl> <fct> <dbl> <fct> <dbl>
1 1 TRAINING M 74 18 ADAS_CogT0 14.7 ADAS_CogT7 16
2 2 TRAINING F 76 4 ADAS_CogT0 15.3 ADAS_CogT7 9.32
3 3 TRAINING M 81 8 ADAS_CogT0 17.3 ADAS_CogT7 21.3
4 4 TRAINING F 74 5 ADAS_CogT0 19 ADAS_CogT7 17
5 5 TRAINING M 69 8 ADAS_CogT0 7.66 ADAS_CogT7 8.32
6 6 TRAINING F 72 11 ADAS_CogT0 12.6 ADAS_CogT7 11
7 7 TRAINING F 75 5 ADAS_CogT0 18.7 ADAS_CogT7 15.0
8 8 TRAINING M 83 5 ADAS_CogT0 15.0 ADAS_CogT7 11.0
9 9 TRAINING F 78 4 ADAS_CogT0 18.0 ADAS_CogT7 17
10 10 TRAINING F 72 8 ADAS_CogT0 17.3 ADAS_CogT7 18.3
Can anyone know how could I do to create a unique column for tests with the correspondent score aside?
Here the dataset I've used
dput(head(db, 100))
structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
93, 94, 95, 96, 97, 98, 99, 100), GROUP = c("TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO"), Gender = c(1,
0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1,
0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1,
0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1,
0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0), Age = c(74, 76,
81, 74, 69, 72, 75, 83, 78, 72, 82, 68, 72, 72, 73, 80, 69, 72,
70, 80, 75, 80, 78, 74, 82, 74, 80, 82, 78, 81, 66, 71, 70, 79,
78, 73, 72, 77, 77, 71, 83, 74, 70, 71, 77, 69, 67, 64, 79, 71,
77, 77, 73, 67, 68, 79, 81, 67, 84, 75, 80, 73, 68, 74, 77, 79,
79, 72, 73, 78, 76, 78, 77, 74, 78, 77, 77, 82, 77, 70, 77, 81,
79, 75, 74, 78, 69, 77, 73, 77, 70, 79, 70, 72, 77, 72, 71, 71,
73, 81), Education = c(18, 4, 8, 5, 8, 11, 5, 5, 4, 8, 8, 12,
5, 18, 13, 5, 13, 13, 5, 5, 13, 5, 3, 8, 17, 5, 8, 5, 5, 8, 17,
8, 18, 18, 13, 13, 13, 13, 15, 17, 8, 5, 5, 13, 8, 5, 11, 13,
8, 8, 8, 5, 13, 8, 5, 17, 8, 12, 13, 5, 8, 8, 8, 5, 3, 8, 18,
5, 8, 13, 8, 5, 17, 8, 5, 17, 5, 8, 11, 8, 8, 5, 12, 3, 8, 8,
8, 13, 5, 5, 8, 8, 13, 5, 5, 8, 13, 5, 8, 12), ADAS_CogT0 = c(14.66,
15.33, 17.33, 19, 7.66, 12.6, 18.67, 14.99, 17.99, 17.33, 13.66,
16.99, 10.66, 9.66, 14.99, 15.66, 13.33, 4.33, 14.33, 15.99,
16.33, 10.66, 14.66, 10.66, 19.33, 17.66, 15.99, 20.66, 20.6,
17, 10.33, 6.33, 6.66, 19.99, 13.33, 24.33, 12.33, 10.33, 12.33,
9.66, 10.99, 13.99, 23, 6.32, 11.32, 13.99, 14.66, 8.99, 14.33,
9.99, 7.33, 15.66, 14, 7.99, 23.32, 14.66, 9.99, 5.66, 6.99,
11.66, 10.33, 6.99, 19.32, NA, 10, 17.66, 13.66, 10.32, NA, NA,
8.66, 9, 6.99, 14.99, 9.66, 13.66, 15.32, 12, 14, 13.66, 11.99,
15.66, 16, 15, 16.99, 20, 11, 7.99, 8.33, 8.32, 14.99, 18.66,
10.33, 11.99, 9.32, 17, 14.33, 14.66, 16.6, 9.99), ROCF_CT0 = c(32,
13.5, 34.7, 26.1, 35.4, 24.6, 28.9, 34.1, 12.1, 28.1, 22.7, 31.6,
30.4, 34.4, 37.2, 38.1, 18.6, 26.2, 34.8, 37.1, 26.2, 35.1, 37.1,
36, 11.2, 17.4, 32.7, 28.4, 21.1, 26.7, 31.9, 33.4, 27.4, 32.7,
33.9, 37.2, 28.2, 34.2, 29.7, 30, 16.2, 37.4, 34.8, 36.6, 33,
30.8, 34.6, 35.1, 29.7, 36.8, 28, 34.4, 33.7, 28.9, 31.8, 31.7,
10.2, 27.1, 36.8, 22.4, 35.7, 28, 30.9, 35.4, 26.9, 29.7, 26.7,
28.8, 32, 37.9, 27, 18.6, 33, 38, 25.1, 31, 27.1, 26.7, 37, 30.4,
37, 30.1, 36.9, 24.4, 34, 6.7, 20.4, 30.2, 31.1, 33.4, 17.4,
23.7, 30.6, 28.3, 19.4, 33.4, 31.6, 39.8, 27, 33.9), ROCF_IT0 = c(3.7,
7.6, 14.9, 13.6, 23.4, 4, 8.1, 23.9, 10.1, 7, 10, 9.2, 15.6,
10.8, 6.6, 13.6, 4, 15.3, 7.4, 0, 8.6, 22.9, 11.6, 10.1, 6.8,
12.1, 12.9, 0, 10.6, 5.9, 14.8, 10.9, 10.6, 10.6, 7.5, 7.3, 4.1,
3.1, 7.6, 13.4, 10.9, 19, 3.4, 11.7, 14.6, 9.4, 16.7, 15.7, 14.5,
12.5, 15.1, 22.6, 9.1, 15.5, 11, 6.1, 11.5, 5.6, 17.2, 18.1,
23.5, 15.6, 7.7, 13.5, 14.5, 11.9, 10.3, 11.5, 12.6, 19, 11.9,
15.4, 17.7, 8.4, 13.4, 5.2, 11.1, 16, 10.9, 8, 22.6, 13.6, 12.2,
6.5, 13.4, 11.5, 8, 18.1, 10, 11, 5.9, 14.9, 12, 11.4, 17, 5.9,
15, 10.9, 8.9, 15.7), ROCF_RT0 = c(3.9, 7.8, 16.3, 13.4, 24.1,
4.7, 7.8, 20.9, 9.8, 5.1, 9.8, 8.2, 16.3, 11.3, 5.9, 14.8, 4.7,
14.4, 6.7, 0, 4.9, 23.3, 14.8, 11.7, 6.4, 10.3, 14.3, 0, 10.8,
6.3, 15.4, 10.1, 5.3, 12, 6.9, 5.4, 4.4, 4.4, 7, 10.9, 11.9,
17.3, 3.7, 12.2, 6.7, 14.7, 16.2, 7.8, 10.8, 10.2, 16.7, 22.8,
9.1, 15.7, 10.7, 4, 8.8, 6.8, 14.6, 14.5, 24.8, 13.7, 0, 10.3,
17.3, 11.3, 8, 13.2, 15.2, 18.4, 4.3, 12.8, 18.9, 4.3, 11.8,
4.9, 11.8, 13.3, 10.3, 7.1, 23.7, 15.8, 16.4, 6.8, 20.3, 11.3,
7.6, 19.9, 10.8, 12.5, 5.6, 11.3, 11.7, 13.5, 12.3, 5.1, 14.2,
8.7, 12.3, 16.4), PVF_T0 = c(41.3, 35.4, 16.7, 28.4, 46.9, 33.6,
39.4, 27.4, 17.4, 37.9, 31.7, 37.6, 33.4, 21.5, 49.5, 40.4, 10.6,
40.5, 30.6, 28.4, 29.5, 22.4, 45.4, 29.7, 17.3, 39.4, 31.7, 24.4,
38.4, 22.7, 6.8, 19.9, 56.5, 35.3, 43.5, 32.5, 28.5, 38.5, 22.3,
32.3, 30.7, 23.4, 18.6, 46.6, 36.7, 35.6, 38.6, 16.9, 38.7, 37.6,
39.7, 34.4, 27.5, 16.2, 32.6, 48.3, 26.7, 49.9, 40.5, 34.4, 37.7,
39.7, 23.2, 19.4, 21.4, 33.7, 11.3, 41.6, 23.7, 31.5, 28.7, 34.4,
33.3, 27.7, 23.4, 32.3, 49.4, 35.7, 26.7, 31.9, 37.7, 35.4, 20.5,
28.4, 14.7, 30.7, 35.9, 16.5, 27.4, 49.4, 37.9, 27.7, 41.6, 31.6,
55.4, 42.9, 21.6, 25.6, 26.7, 43.5), SVF_T0 = c(40, 37, 30, 28,
49, 27, 34, 35, 29, 33, 20, 18, 36, 47, 43, 39, 36, 38, 48, 31,
25, 34, 31, 33, 24, 30, 45, 20, 33, 34, 29, 42, 40, 37, 37, 25,
28, 42, 19, 32, 37, 35, 20, 43, 33, 40, 40, 42, 27, 35, 29, 39,
31, 48, 41, 27, 32, 45, 39, 40, 36, 38, 27, 32, 47, 36, 24, 40,
38, 29, 43, 47, 38, 33, 44, 39, 46, 36, 32, 38, 38, 35, 26, 43,
29, 24, 33, 30, 33, 57, 40, 37, 44, 34, 46, 41, 33, 35, 29, 37
), ADAS_CogT7 = c(16, 9.32, 21.33, 17, 8.32, 11, 14.99, 10.99,
17, 18.33, 13.32, 14.34, 8.99, 7, 11.99, 15.33, 6.99, 5.33, 12.32,
13, 21.32, 7.99, 13.33, 11.99, 17.32, 16.32, 16.33, 14.66, 18.99,
17.33, 7.99, 9.33, 10.99, NA, 12.99, 16.33, 21.66, 9, 9.34, 8.66,
8.33, 13.66, 15.66, 6.66, 10.99, 13.33, 13.33, 7.99, 11.99, 11.32,
7.33, 9.66, 6.99, NA, 15.99, 15.66, 14.66, 6.32, 7, 11, 14, 10.33,
24.66, NA, 14.99, NA, 15.99, 9.32, NA, NA, 9.99, 9.33, 7.66,
17.33, 10.32, 16, 17, 12.99, 15, 14.33, 10, 14.99, 19, 13.99,
19.33, NA, 10, 6.99, 11.66, 6.66, 14.33, 16, 8.66, 10, NA, 20,
14.99, 19.66, 26.66, 8.99), ROCF_CT7 = c(33, 23.9, 28.7, 39.1,
36.4, 29.2, 11.4, 38.1, 18.1, 28.4, 22.2, 18.6, 37, 35, 33.2,
23.6, 21.1, 30.2, 28.8, 37.1, 32.2, 33.1, 26.1, 38, 23.6, 21.4,
37.3, 36.4, 15.1, 35.7, 29.9, 31.4, 32.4, NA, 37.9, 28.2, 37.2,
31.9, 36.7, 24.5, 19.2, 23.4, 32.8, 36.2, 31.7, 24.3, 31.6, 33.1,
19.2, 30.4, 32.7, 24.4, 36.2, NA, 28.1, 31.7, 19.7, 31.6, 28.3,
27.4, 37.7, 35, 33.4, 34.4, 32.1, NA, 33.7, 31.8, NA, 34.9, 15,
20.1, 31, 35, 37.1, 29, 32.6, 31.7, 28.4, 34.4, 35.7, 35.1, 35.9,
27.4, 34, NA, 17.4, 28.9, 35.4, 33.4, 30.4, 14.7, 33.2, 26.8,
NA, 20, 33.6, 26.8, 36, 34.9), ROCF_IT7 = c(1.7, 12.1, 5.9, 12.1,
24.4, 9.8, 7.6, 24.4, 9.6, 7, 11.5, 9.7, 17.1, 14.2, 4.6, 12.1,
4.5, 18.8, 8.4, 14.6, 6.1, 17.1, 15.1, 13.1, 11.8, 12.1, 15.9,
7.6, 11.1, 5.9, 23.8, 12.4, 11.1, NA, 11, 7.8, 5.6, 6.5, 10.1,
9.9, 14.4, 17, 6.4, 27.3, 16, 14.4, 19.7, 16.2, 8.5, 8, 20.5,
15.1, 15.6, NA, 9, 8.6, 8.5, 13.2, 17.7, 14.1, 11, 9.1, 7, 16,
20.4, NA, 12.3, 16, NA, 15, 8.4, 17.9, 19.7, 11.9, 18.6, 4.7,
12.1, 17.5, 9, 5.5, 17.4, 12.1, 9.7, 9.5, 13.4, NA, 7.5, 14.5,
11, 19.5, 4.9, 11.9, 26, 12, NA, 3.9, 5.5, 6.4, 4.9, 16.2), ROCF_RT7 = c(2.4,
9.3, 6.3, 12.4, 22.6, 8.9, 7.8, 21.9, 9.8, 5.1, 10.8, 4.2, 12.3,
18.9, 2.9, 13.8, 1.2, 19.9, 4.2, 9.8, 5.9, 21.3, 12.3, 10.2,
12.1, 7.8, 6.3, 7.8, 9.8, 6.3, 16.9, 11.6, 11.3, NA, 8.9, 16.9,
3.9, 6.4, 13, 9.9, 14.9, 18.4, 3.7, 15.4, 8.8, 14.2, 14.7, 14.3,
8.8, 5.1, 21.8, 14.3, 11.4, NA, 8.7, 9.5, 8.8, 14.2, 17, 13.8,
12.8, 9.7, 5.1, 15.3, 22.8, NA, 6.4, 12.7, NA, 20.9, 7.8, 11.8,
22.4, 4.3, 17.8, 4.9, 13.8, 16.4, 7.4, 5.6, 24.8, 11.8, 9.4,
11.8, 20.3, NA, 7.1, 13.9, 12.3, 23.5, 2.6, 14.8, 15.2, 11.7,
NA, 4.3, 6.7, 5.2, 7.3, 18.4), PVF_T7 = c(38.3, 49.4, 16.7, 31.4,
60.9, 29.5, 48.4, 28.4, 17.4, 32.9, 40.7, 42.6, 42.4, 32.3, 38.5,
40.4, 20.4, 40.5, 42.6, 28.4, 27.5, 24.4, 40.4, 28.7, 5.3, 38.4,
46.7, 26.4, 37.4, 23.7, 9.8, 34.9, 55.5, NA, 38.5, 38.5, 25.5,
43.5, 36.3, 30.3, 32.7, 15.4, 21.6, 40.5, 38.7, 48.6, 47.6, 26.9,
35.7, 31.9, 47.7, 35.4, 29.5, NA, 33.6, 48.3, 27.7, 55.6, 39.5,
30.4, 36.7, 38.7, 23.9, 26.4, 28.4, NA, 18.3, 47.6, NA, 35.5,
29.7, 39.4, 44.3, 26.7, 29.4, 31.3, 44.4, 30.7, 11.5, 40.9, 37.7,
40.4, 14.5, 21.4, 14.7, NA, 29.9, 20.5, 34.4, 55.4, 32.9, 20.7,
27.6, 31.6, NA, 7.7, 25.6, 22.6, 22.7, 34.5), SVF_T7 = c(26,
48, 30, 29, 43, 25, 42, 36, 31, 27, 22, 21, 40, 46, 32, 29, 32,
38, 46, 31, 20, 36, 35, 32, 12, 28, 47, 20, 33, 30, 22, 44, 35,
NA, 36, 21, 24, 45, 25, 25, 30, 27, 34, 50, 31, 42, 34, 40, 25,
31, 41, 37, 38, NA, 38, 19, 29, 46, 40, 38, 36, 34, 31, 40, 39,
NA, 19, 40, NA, 29, 44, 43, 43, 31, 45, 28, 35, 31, 33, 36, 28,
35, 34, 34, 29, NA, 26, 30, 34, 57, 40, 36, 41, 36, NA, 22, 31,
29, 26, 31)), row.names = c(NA, -100L), class = c("tbl_df", "tbl",
"data.frame"))
>
I've preferred to make two separated columns for the two diffrent time points observations but I think it would more confrotable to have just a unique one
Not really sure what you are aiming for but it sounds as if you want the data in long format by test?
library(stringr)
library(tidyr)
library(dplyr)
# extract variables associated with tests
nms_test <- names(db)[6:17]
# create new names which will be helpful when changing to long format
nms_new <- paste0(str_remove(str_extract(nms_test, "[A-z_]+(?=T\\d$)"), "_$"),
"-",
paste0(str_extract(nms_test, "T\\d$")))
# create a named vector for the new names
names(nms_test) <- nms_new
db %>%
rename(nms_test) %>%
pivot_longer(-c(1:5),
names_to = c(".value", "test"),
names_sep = "-")
#> # A tibble: 6 x 12
#> ID GROUP Gender Age Education test ADAS_Cog ROCF_C ROCF_I ROCF_R PVF
#> <dbl> <chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 TRAINI~ 1 74 18 T0 14.7 32 3.7 3.9 41.3
#> 2 1 TRAINI~ 1 74 18 T7 16 33 1.7 2.4 38.3
#> 3 2 TRAINI~ 0 76 4 T0 15.3 13.5 7.6 7.8 35.4
#> 4 2 TRAINI~ 0 76 4 T7 9.32 23.9 12.1 9.3 49.4
#> 5 3 TRAINI~ 1 81 8 T0 17.3 34.7 14.9 16.3 16.7
#> 6 3 TRAINI~ 1 81 8 T7 21.3 28.7 5.9 6.3 16.7
#> # ... with 1 more variable: SVF <dbl>
Alternatively if I understand your later comment correctly you want all tests and test results in two columns, which is simpler!
db1 <-
db %>%
pivot_longer(-c(1:5),
names_to = "test",
values_to = "value")
head(db1, 10)
#> # A tibble: 10 x 7
#> ID GROUP Gender Age Education test value
#> <dbl> <chr> <dbl> <dbl> <dbl> <chr> <dbl>
#> 1 1 TRAINING 1 74 18 ADAS_CogT0 14.7
#> 2 1 TRAINING 1 74 18 ROCF_CT0 32
#> 3 1 TRAINING 1 74 18 ROCF_IT0 3.7
#> 4 1 TRAINING 1 74 18 ROCF_RT0 3.9
#> 5 1 TRAINING 1 74 18 PVF_T0 41.3
#> 6 1 TRAINING 1 74 18 SVF_T0 40
#> 7 1 TRAINING 1 74 18 ADAS_CogT7 16
#> 8 1 TRAINING 1 74 18 ROCF_CT7 33
#> 9 1 TRAINING 1 74 18 ROCF_IT7 1.7
#> 10 1 TRAINING 1 74 18 ROCF_RT7 2.4
Created on 2021-11-26 by the reprex package (v2.0.1)

Reproduce a complex table with double headesrs

Locked for 351 days. There are disputes about this question’s content being resolved at this time. It is not currently accepting new answers or interactions.
I would like to create the following table
Where
Alzheimer’s Disease Assessment Scale-Cognitive = ADAS_CogT0
Rey-Osterrieth Complex Figure Test immediate recall = ROCF_IT0
Copy of Rey-Osterrieth Complex Figure test = ROCF_CT0
Rey-Osterrieth Complex Figure Test delayed recall = ROCf_RT0
Phonemic verbal fluency = PVF_T0
Semantic verbal fluency = SVF_T0
I have started manipulating data as follows:
library(tidyverse)
library(readxl)
library(rms)
library(lme4)
library(flextable)
library(gtsummary)
library(data.table)
library(plotrix)
library(purrr)
out <db %>%
mutate(GROUP = fct_recode(GROUP, CONTROL = "CONTROLLO")) %>%
to_long(keys = c("tests0", "tests7"),
values =c("score0", "score7"),
grep("T0$", names(.), value = TRUE),
grep("T7$", names(.), value = TRUE)) %>%
as_tibble %>%
mutate(Gender = factor(Gender, levels = c(1, 0), labels = c("M", "F")),
across(c(tests0, tests7), factor))
out1 <- out %>%
group_by(tests0, GROUP) %>%
filter(GROUP == 'TRAINING') %>%
summarise(mean0 = mean(score0, na.rm = T),
stderr0 = std.error(score0, na.rm = T),
mean7 = mean(score7, na.rm = T),
stederr7 = std.error(score7, na.rm = T),
diff.std.mean = t.test(score0, score7, paired = T)$estimate,
p.value = t.test(score0, score7, paired = T)$p.value
) %>%
ungroup()
out2 <- out %>%
group_by(tests0, GROUP) %>%
filter(GROUP == 'CONTROL') %>%
summarise(mean0 = mean(score0, na.rm = T),
stderr0 = std.error(score0, na.rm = T),
mean7 = mean(score7, na.rm = T),
stederr7 = std.error(score7, na.rm = T),
diff.std.mean = t.test(score0, score7, paired = T)$estimate,
p.value = t.test(score0, score7, paired = T)$p.value
) %>%
ungroup()
out3 <- out1 %>%
bind_cols(out2) %>%
select(., -'tests0...9') %>%
flextable()
obtaing this results
I got stuck at this point, where I am a bit confuse as to create the final column for the between-group differences that are supposed to contain
mean difference of the of the two MEAN DIFFERENCE COLUMNS you can see in the blank picture and I don't how to enter it into the code. Moreover I am not able create the two headers referred to:
TRAINING and CONTROL groups
WITHIN AND BETWEEN GROUPS
here the dataset
dput(head(db, 100))
structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
93, 94, 95, 96, 97, 98, 99, 100), GROUP = c("TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO"), Gender = c(1,
0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1,
0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1,
0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1,
0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0), Age = c(74, 76,
81, 74, 69, 72, 75, 83, 78, 72, 82, 68, 72, 72, 73, 80, 69, 72,
70, 80, 75, 80, 78, 74, 82, 74, 80, 82, 78, 81, 66, 71, 70, 79,
78, 73, 72, 77, 77, 71, 83, 74, 70, 71, 77, 69, 67, 64, 79, 71,
77, 77, 73, 67, 68, 79, 81, 67, 84, 75, 80, 73, 68, 74, 77, 79,
79, 72, 73, 78, 76, 78, 77, 74, 78, 77, 77, 82, 77, 70, 77, 81,
79, 75, 74, 78, 69, 77, 73, 77, 70, 79, 70, 72, 77, 72, 71, 71,
73, 81), Education = c(18, 4, 8, 5, 8, 11, 5, 5, 4, 8, 8, 12,
5, 18, 13, 5, 13, 13, 5, 5, 13, 5, 3, 8, 17, 5, 8, 5, 5, 8, 17,
8, 18, 18, 13, 13, 13, 13, 15, 17, 8, 5, 5, 13, 8, 5, 11, 13,
8, 8, 8, 5, 13, 8, 5, 17, 8, 12, 13, 5, 8, 8, 8, 5, 3, 8, 18,
5, 8, 13, 8, 5, 17, 8, 5, 17, 5, 8, 11, 8, 8, 5, 12, 3, 8, 8,
8, 13, 5, 5, 8, 8, 13, 5, 5, 8, 13, 5, 8, 12), ADAS_CogT0 = c(14.66,
15.33, 17.33, 19, 7.66, 12.6, 18.67, 14.99, 17.99, 17.33, 13.66,
16.99, 10.66, 9.66, 14.99, 15.66, 13.33, 4.33, 14.33, 15.99,
16.33, 10.66, 14.66, 10.66, 19.33, 17.66, 15.99, 20.66, 20.6,
17, 10.33, 6.33, 6.66, 19.99, 13.33, 24.33, 12.33, 10.33, 12.33,
9.66, 10.99, 13.99, 23, 6.32, 11.32, 13.99, 14.66, 8.99, 14.33,
9.99, 7.33, 15.66, 14, 7.99, 23.32, 14.66, 9.99, 5.66, 6.99,
11.66, 10.33, 6.99, 19.32, NA, 10, 17.66, 13.66, 10.32, NA, NA,
8.66, 9, 6.99, 14.99, 9.66, 13.66, 15.32, 12, 14, 13.66, 11.99,
15.66, 16, 15, 16.99, 20, 11, 7.99, 8.33, 8.32, 14.99, 18.66,
10.33, 11.99, 9.32, 17, 14.33, 14.66, 16.6, 9.99), ROCF_CT0 = c(32,
13.5, 34.7, 26.1, 35.4, 24.6, 28.9, 34.1, 12.1, 28.1, 22.7, 31.6,
30.4, 34.4, 37.2, 38.1, 18.6, 26.2, 34.8, 37.1, 26.2, 35.1, 37.1,
36, 11.2, 17.4, 32.7, 28.4, 21.1, 26.7, 31.9, 33.4, 27.4, 32.7,
33.9, 37.2, 28.2, 34.2, 29.7, 30, 16.2, 37.4, 34.8, 36.6, 33,
30.8, 34.6, 35.1, 29.7, 36.8, 28, 34.4, 33.7, 28.9, 31.8, 31.7,
10.2, 27.1, 36.8, 22.4, 35.7, 28, 30.9, 35.4, 26.9, 29.7, 26.7,
28.8, 32, 37.9, 27, 18.6, 33, 38, 25.1, 31, 27.1, 26.7, 37, 30.4,
37, 30.1, 36.9, 24.4, 34, 6.7, 20.4, 30.2, 31.1, 33.4, 17.4,
23.7, 30.6, 28.3, 19.4, 33.4, 31.6, 39.8, 27, 33.9), ROCF_IT0 = c(3.7,
7.6, 14.9, 13.6, 23.4, 4, 8.1, 23.9, 10.1, 7, 10, 9.2, 15.6,
10.8, 6.6, 13.6, 4, 15.3, 7.4, 0, 8.6, 22.9, 11.6, 10.1, 6.8,
12.1, 12.9, 0, 10.6, 5.9, 14.8, 10.9, 10.6, 10.6, 7.5, 7.3, 4.1,
3.1, 7.6, 13.4, 10.9, 19, 3.4, 11.7, 14.6, 9.4, 16.7, 15.7, 14.5,
12.5, 15.1, 22.6, 9.1, 15.5, 11, 6.1, 11.5, 5.6, 17.2, 18.1,
23.5, 15.6, 7.7, 13.5, 14.5, 11.9, 10.3, 11.5, 12.6, 19, 11.9,
15.4, 17.7, 8.4, 13.4, 5.2, 11.1, 16, 10.9, 8, 22.6, 13.6, 12.2,
6.5, 13.4, 11.5, 8, 18.1, 10, 11, 5.9, 14.9, 12, 11.4, 17, 5.9,
15, 10.9, 8.9, 15.7), ROCF_RT0 = c(3.9, 7.8, 16.3, 13.4, 24.1,
4.7, 7.8, 20.9, 9.8, 5.1, 9.8, 8.2, 16.3, 11.3, 5.9, 14.8, 4.7,
14.4, 6.7, 0, 4.9, 23.3, 14.8, 11.7, 6.4, 10.3, 14.3, 0, 10.8,
6.3, 15.4, 10.1, 5.3, 12, 6.9, 5.4, 4.4, 4.4, 7, 10.9, 11.9,
17.3, 3.7, 12.2, 6.7, 14.7, 16.2, 7.8, 10.8, 10.2, 16.7, 22.8,
9.1, 15.7, 10.7, 4, 8.8, 6.8, 14.6, 14.5, 24.8, 13.7, 0, 10.3,
17.3, 11.3, 8, 13.2, 15.2, 18.4, 4.3, 12.8, 18.9, 4.3, 11.8,
4.9, 11.8, 13.3, 10.3, 7.1, 23.7, 15.8, 16.4, 6.8, 20.3, 11.3,
7.6, 19.9, 10.8, 12.5, 5.6, 11.3, 11.7, 13.5, 12.3, 5.1, 14.2,
8.7, 12.3, 16.4), PVF_T0 = c(41.3, 35.4, 16.7, 28.4, 46.9, 33.6,
39.4, 27.4, 17.4, 37.9, 31.7, 37.6, 33.4, 21.5, 49.5, 40.4, 10.6,
40.5, 30.6, 28.4, 29.5, 22.4, 45.4, 29.7, 17.3, 39.4, 31.7, 24.4,
38.4, 22.7, 6.8, 19.9, 56.5, 35.3, 43.5, 32.5, 28.5, 38.5, 22.3,
32.3, 30.7, 23.4, 18.6, 46.6, 36.7, 35.6, 38.6, 16.9, 38.7, 37.6,
39.7, 34.4, 27.5, 16.2, 32.6, 48.3, 26.7, 49.9, 40.5, 34.4, 37.7,
39.7, 23.2, 19.4, 21.4, 33.7, 11.3, 41.6, 23.7, 31.5, 28.7, 34.4,
33.3, 27.7, 23.4, 32.3, 49.4, 35.7, 26.7, 31.9, 37.7, 35.4, 20.5,
28.4, 14.7, 30.7, 35.9, 16.5, 27.4, 49.4, 37.9, 27.7, 41.6, 31.6,
55.4, 42.9, 21.6, 25.6, 26.7, 43.5), SVF_T0 = c(40, 37, 30, 28,
49, 27, 34, 35, 29, 33, 20, 18, 36, 47, 43, 39, 36, 38, 48, 31,
25, 34, 31, 33, 24, 30, 45, 20, 33, 34, 29, 42, 40, 37, 37, 25,
28, 42, 19, 32, 37, 35, 20, 43, 33, 40, 40, 42, 27, 35, 29, 39,
31, 48, 41, 27, 32, 45, 39, 40, 36, 38, 27, 32, 47, 36, 24, 40,
38, 29, 43, 47, 38, 33, 44, 39, 46, 36, 32, 38, 38, 35, 26, 43,
29, 24, 33, 30, 33, 57, 40, 37, 44, 34, 46, 41, 33, 35, 29, 37
), ADAS_CogT7 = c(16, 9.32, 21.33, 17, 8.32, 11, 14.99, 10.99,
17, 18.33, 13.32, 14.34, 8.99, 7, 11.99, 15.33, 6.99, 5.33, 12.32,
13, 21.32, 7.99, 13.33, 11.99, 17.32, 16.32, 16.33, 14.66, 18.99,
17.33, 7.99, 9.33, 10.99, NA, 12.99, 16.33, 21.66, 9, 9.34, 8.66,
8.33, 13.66, 15.66, 6.66, 10.99, 13.33, 13.33, 7.99, 11.99, 11.32,
7.33, 9.66, 6.99, NA, 15.99, 15.66, 14.66, 6.32, 7, 11, 14, 10.33,
24.66, NA, 14.99, NA, 15.99, 9.32, NA, NA, 9.99, 9.33, 7.66,
17.33, 10.32, 16, 17, 12.99, 15, 14.33, 10, 14.99, 19, 13.99,
19.33, NA, 10, 6.99, 11.66, 6.66, 14.33, 16, 8.66, 10, NA, 20,
14.99, 19.66, 26.66, 8.99), ROCF_CT7 = c(33, 23.9, 28.7, 39.1,
36.4, 29.2, 11.4, 38.1, 18.1, 28.4, 22.2, 18.6, 37, 35, 33.2,
23.6, 21.1, 30.2, 28.8, 37.1, 32.2, 33.1, 26.1, 38, 23.6, 21.4,
37.3, 36.4, 15.1, 35.7, 29.9, 31.4, 32.4, NA, 37.9, 28.2, 37.2,
31.9, 36.7, 24.5, 19.2, 23.4, 32.8, 36.2, 31.7, 24.3, 31.6, 33.1,
19.2, 30.4, 32.7, 24.4, 36.2, NA, 28.1, 31.7, 19.7, 31.6, 28.3,
27.4, 37.7, 35, 33.4, 34.4, 32.1, NA, 33.7, 31.8, NA, 34.9, 15,
20.1, 31, 35, 37.1, 29, 32.6, 31.7, 28.4, 34.4, 35.7, 35.1, 35.9,
27.4, 34, NA, 17.4, 28.9, 35.4, 33.4, 30.4, 14.7, 33.2, 26.8,
NA, 20, 33.6, 26.8, 36, 34.9), ROCF_IT7 = c(1.7, 12.1, 5.9, 12.1,
24.4, 9.8, 7.6, 24.4, 9.6, 7, 11.5, 9.7, 17.1, 14.2, 4.6, 12.1,
4.5, 18.8, 8.4, 14.6, 6.1, 17.1, 15.1, 13.1, 11.8, 12.1, 15.9,
7.6, 11.1, 5.9, 23.8, 12.4, 11.1, NA, 11, 7.8, 5.6, 6.5, 10.1,
9.9, 14.4, 17, 6.4, 27.3, 16, 14.4, 19.7, 16.2, 8.5, 8, 20.5,
15.1, 15.6, NA, 9, 8.6, 8.5, 13.2, 17.7, 14.1, 11, 9.1, 7, 16,
20.4, NA, 12.3, 16, NA, 15, 8.4, 17.9, 19.7, 11.9, 18.6, 4.7,
12.1, 17.5, 9, 5.5, 17.4, 12.1, 9.7, 9.5, 13.4, NA, 7.5, 14.5,
11, 19.5, 4.9, 11.9, 26, 12, NA, 3.9, 5.5, 6.4, 4.9, 16.2), ROCF_RT7 = c(2.4,
9.3, 6.3, 12.4, 22.6, 8.9, 7.8, 21.9, 9.8, 5.1, 10.8, 4.2, 12.3,
18.9, 2.9, 13.8, 1.2, 19.9, 4.2, 9.8, 5.9, 21.3, 12.3, 10.2,
12.1, 7.8, 6.3, 7.8, 9.8, 6.3, 16.9, 11.6, 11.3, NA, 8.9, 16.9,
3.9, 6.4, 13, 9.9, 14.9, 18.4, 3.7, 15.4, 8.8, 14.2, 14.7, 14.3,
8.8, 5.1, 21.8, 14.3, 11.4, NA, 8.7, 9.5, 8.8, 14.2, 17, 13.8,
12.8, 9.7, 5.1, 15.3, 22.8, NA, 6.4, 12.7, NA, 20.9, 7.8, 11.8,
22.4, 4.3, 17.8, 4.9, 13.8, 16.4, 7.4, 5.6, 24.8, 11.8, 9.4,
11.8, 20.3, NA, 7.1, 13.9, 12.3, 23.5, 2.6, 14.8, 15.2, 11.7,
NA, 4.3, 6.7, 5.2, 7.3, 18.4), PVF_T7 = c(38.3, 49.4, 16.7, 31.4,
60.9, 29.5, 48.4, 28.4, 17.4, 32.9, 40.7, 42.6, 42.4, 32.3, 38.5,
40.4, 20.4, 40.5, 42.6, 28.4, 27.5, 24.4, 40.4, 28.7, 5.3, 38.4,
46.7, 26.4, 37.4, 23.7, 9.8, 34.9, 55.5, NA, 38.5, 38.5, 25.5,
43.5, 36.3, 30.3, 32.7, 15.4, 21.6, 40.5, 38.7, 48.6, 47.6, 26.9,
35.7, 31.9, 47.7, 35.4, 29.5, NA, 33.6, 48.3, 27.7, 55.6, 39.5,
30.4, 36.7, 38.7, 23.9, 26.4, 28.4, NA, 18.3, 47.6, NA, 35.5,
29.7, 39.4, 44.3, 26.7, 29.4, 31.3, 44.4, 30.7, 11.5, 40.9, 37.7,
40.4, 14.5, 21.4, 14.7, NA, 29.9, 20.5, 34.4, 55.4, 32.9, 20.7,
27.6, 31.6, NA, 7.7, 25.6, 22.6, 22.7, 34.5), SVF_T7 = c(26,
48, 30, 29, 43, 25, 42, 36, 31, 27, 22, 21, 40, 46, 32, 29, 32,
38, 46, 31, 20, 36, 35, 32, 12, 28, 47, 20, 33, 30, 22, 44, 35,
NA, 36, 21, 24, 45, 25, 25, 30, 27, 34, 50, 31, 42, 34, 40, 25,
31, 41, 37, 38, NA, 38, 19, 29, 46, 40, 38, 36, 34, 31, 40, 39,
NA, 19, 40, NA, 29, 44, 43, 43, 31, 45, 28, 35, 31, 33, 36, 28,
35, 34, 34, 29, NA, 26, 30, 34, 57, 40, 36, 41, 36, NA, 22, 31,
29, 26, 31)), row.names = c(NA, -100L), class = c("tbl_df", "tbl",
"data.frame"))
>
I could not reproduce your code as some datasets are missing.
Below an example with various method to add header rows.
library(palmerpenguins)
library(tidyverse)
library(flextable)
by <- c("species", "island")
tabl <- palmerpenguins::penguins %>%
group_by(across(all_of(by))) %>%
summarise(
across(ends_with("mm"),
list(
mean = ~ mean(.x, na.rm = TRUE),
median = ~ median(.x, na.rm = TRUE),
sd = ~ sd(.x, na.rm = TRUE)
)
),
.groups = "drop"
)
tab_header <- tibble( key = colnames(tabl)) %>%
separate(col = key, into = c("part", "measure", "unit", "stat"),
fill = "right", remove = FALSE) %>%
select(-unit) %>%
mutate(
measure = if_else(is.na(measure), part, measure),
stat = if_else(is.na(stat), measure, stat)
)
tab_header
flextable(tabl) %>%
flextable::set_header_df(tab_header, key = "key") %>%
merge_h(part = "header") %>%
merge_at(part = "header", i = 1:3, j = 1) %>%
merge_at(part = "header", i = 1:3, j = 2) %>%
theme_box() %>%
add_header_row(values = c("", "bada", "boum"), colwidths = c(1, 5, 5)) %>%
add_header_lines(c("another way to add a row of text")) %>%
align(part = "header", i = seq_len(nrow_part(., part = "header")-1), align = "center")

Redundant elements to simplify in the following code manipulation

Locked for 351 days. There are disputes about this question’s content being resolved at this time. It is not currently accepting new answers or interactions.
I have tried to use the dplyr library to merge to different spreadsheet and make the following manipulation.
db0 <- t0 %>%
mutate(GROUP = ifelse(GROUP == 'CONTROLLO', 'CONTROL','TRAINING')) %>%
mutate(GROUP = factor(GROUP, levels = c('CONTROL' ,"TRAINING"))) %>%
pivot_longer(c(ADAS_CogT0:SVF_T0), names_to = 'tests0', values_to = 'score0') %>%
arrange(tests0) %>%
pivot_longer(c(ADAS_CogT7:SVF_T7), names_to = 'tests7', values_to = 'score7') %>%
arrange(tests7) %>%
mutate(
Gender = factor(if_else(
Gender == "1", "M", "F"
), levels = c("M", "F"))
) %>% mutate(tests0 = factor(tests0,
levels = c('ADAS_CogT0',
'PVF_T0', 'ROCF_CT0', 'ROCF_IT0',
'ROCF_RT0', 'SVF_T0'))) %>%
mutate(tests7 = factor(tests7,
levels = c('ADAS_CogT7',
'PVF_T7', 'ROCF_CT7', 'ROCF_IT7',
'ROCF_RT7', 'SVF_T7')))
In your personal view, is there any steps in this code that could be simply? Any redundancies that could be removed. In any case I report here the already merged dataset
dput(head(db, 100))
structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
93, 94, 95, 96, 97, 98, 99, 100), GROUP = c("TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING",
"TRAINING", "TRAINING", "TRAINING", "TRAINING", "TRAINING", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO",
"CONTROLLO", "CONTROLLO", "CONTROLLO", "CONTROLLO"), Gender = c(1,
0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1,
0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1,
0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1,
0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0), Age = c(74, 76,
81, 74, 69, 72, 75, 83, 78, 72, 82, 68, 72, 72, 73, 80, 69, 72,
70, 80, 75, 80, 78, 74, 82, 74, 80, 82, 78, 81, 66, 71, 70, 79,
78, 73, 72, 77, 77, 71, 83, 74, 70, 71, 77, 69, 67, 64, 79, 71,
77, 77, 73, 67, 68, 79, 81, 67, 84, 75, 80, 73, 68, 74, 77, 79,
79, 72, 73, 78, 76, 78, 77, 74, 78, 77, 77, 82, 77, 70, 77, 81,
79, 75, 74, 78, 69, 77, 73, 77, 70, 79, 70, 72, 77, 72, 71, 71,
73, 81), Education = c(18, 4, 8, 5, 8, 11, 5, 5, 4, 8, 8, 12,
5, 18, 13, 5, 13, 13, 5, 5, 13, 5, 3, 8, 17, 5, 8, 5, 5, 8, 17,
8, 18, 18, 13, 13, 13, 13, 15, 17, 8, 5, 5, 13, 8, 5, 11, 13,
8, 8, 8, 5, 13, 8, 5, 17, 8, 12, 13, 5, 8, 8, 8, 5, 3, 8, 18,
5, 8, 13, 8, 5, 17, 8, 5, 17, 5, 8, 11, 8, 8, 5, 12, 3, 8, 8,
8, 13, 5, 5, 8, 8, 13, 5, 5, 8, 13, 5, 8, 12), ADAS_CogT0 = c(14.66,
15.33, 17.33, 19, 7.66, 12.6, 18.67, 14.99, 17.99, 17.33, 13.66,
16.99, 10.66, 9.66, 14.99, 15.66, 13.33, 4.33, 14.33, 15.99,
16.33, 10.66, 14.66, 10.66, 19.33, 17.66, 15.99, 20.66, 20.6,
17, 10.33, 6.33, 6.66, 19.99, 13.33, 24.33, 12.33, 10.33, 12.33,
9.66, 10.99, 13.99, 23, 6.32, 11.32, 13.99, 14.66, 8.99, 14.33,
9.99, 7.33, 15.66, 14, 7.99, 23.32, 14.66, 9.99, 5.66, 6.99,
11.66, 10.33, 6.99, 19.32, NA, 10, 17.66, 13.66, 10.32, NA, NA,
8.66, 9, 6.99, 14.99, 9.66, 13.66, 15.32, 12, 14, 13.66, 11.99,
15.66, 16, 15, 16.99, 20, 11, 7.99, 8.33, 8.32, 14.99, 18.66,
10.33, 11.99, 9.32, 17, 14.33, 14.66, 16.6, 9.99), ROCF_CT0 = c(32,
13.5, 34.7, 26.1, 35.4, 24.6, 28.9, 34.1, 12.1, 28.1, 22.7, 31.6,
30.4, 34.4, 37.2, 38.1, 18.6, 26.2, 34.8, 37.1, 26.2, 35.1, 37.1,
36, 11.2, 17.4, 32.7, 28.4, 21.1, 26.7, 31.9, 33.4, 27.4, 32.7,
33.9, 37.2, 28.2, 34.2, 29.7, 30, 16.2, 37.4, 34.8, 36.6, 33,
30.8, 34.6, 35.1, 29.7, 36.8, 28, 34.4, 33.7, 28.9, 31.8, 31.7,
10.2, 27.1, 36.8, 22.4, 35.7, 28, 30.9, 35.4, 26.9, 29.7, 26.7,
28.8, 32, 37.9, 27, 18.6, 33, 38, 25.1, 31, 27.1, 26.7, 37, 30.4,
37, 30.1, 36.9, 24.4, 34, 6.7, 20.4, 30.2, 31.1, 33.4, 17.4,
23.7, 30.6, 28.3, 19.4, 33.4, 31.6, 39.8, 27, 33.9), ROCF_IT0 = c(3.7,
7.6, 14.9, 13.6, 23.4, 4, 8.1, 23.9, 10.1, 7, 10, 9.2, 15.6,
10.8, 6.6, 13.6, 4, 15.3, 7.4, 0, 8.6, 22.9, 11.6, 10.1, 6.8,
12.1, 12.9, 0, 10.6, 5.9, 14.8, 10.9, 10.6, 10.6, 7.5, 7.3, 4.1,
3.1, 7.6, 13.4, 10.9, 19, 3.4, 11.7, 14.6, 9.4, 16.7, 15.7, 14.5,
12.5, 15.1, 22.6, 9.1, 15.5, 11, 6.1, 11.5, 5.6, 17.2, 18.1,
23.5, 15.6, 7.7, 13.5, 14.5, 11.9, 10.3, 11.5, 12.6, 19, 11.9,
15.4, 17.7, 8.4, 13.4, 5.2, 11.1, 16, 10.9, 8, 22.6, 13.6, 12.2,
6.5, 13.4, 11.5, 8, 18.1, 10, 11, 5.9, 14.9, 12, 11.4, 17, 5.9,
15, 10.9, 8.9, 15.7), ROCF_RT0 = c(3.9, 7.8, 16.3, 13.4, 24.1,
4.7, 7.8, 20.9, 9.8, 5.1, 9.8, 8.2, 16.3, 11.3, 5.9, 14.8, 4.7,
14.4, 6.7, 0, 4.9, 23.3, 14.8, 11.7, 6.4, 10.3, 14.3, 0, 10.8,
6.3, 15.4, 10.1, 5.3, 12, 6.9, 5.4, 4.4, 4.4, 7, 10.9, 11.9,
17.3, 3.7, 12.2, 6.7, 14.7, 16.2, 7.8, 10.8, 10.2, 16.7, 22.8,
9.1, 15.7, 10.7, 4, 8.8, 6.8, 14.6, 14.5, 24.8, 13.7, 0, 10.3,
17.3, 11.3, 8, 13.2, 15.2, 18.4, 4.3, 12.8, 18.9, 4.3, 11.8,
4.9, 11.8, 13.3, 10.3, 7.1, 23.7, 15.8, 16.4, 6.8, 20.3, 11.3,
7.6, 19.9, 10.8, 12.5, 5.6, 11.3, 11.7, 13.5, 12.3, 5.1, 14.2,
8.7, 12.3, 16.4), PVF_T0 = c(41.3, 35.4, 16.7, 28.4, 46.9, 33.6,
39.4, 27.4, 17.4, 37.9, 31.7, 37.6, 33.4, 21.5, 49.5, 40.4, 10.6,
40.5, 30.6, 28.4, 29.5, 22.4, 45.4, 29.7, 17.3, 39.4, 31.7, 24.4,
38.4, 22.7, 6.8, 19.9, 56.5, 35.3, 43.5, 32.5, 28.5, 38.5, 22.3,
32.3, 30.7, 23.4, 18.6, 46.6, 36.7, 35.6, 38.6, 16.9, 38.7, 37.6,
39.7, 34.4, 27.5, 16.2, 32.6, 48.3, 26.7, 49.9, 40.5, 34.4, 37.7,
39.7, 23.2, 19.4, 21.4, 33.7, 11.3, 41.6, 23.7, 31.5, 28.7, 34.4,
33.3, 27.7, 23.4, 32.3, 49.4, 35.7, 26.7, 31.9, 37.7, 35.4, 20.5,
28.4, 14.7, 30.7, 35.9, 16.5, 27.4, 49.4, 37.9, 27.7, 41.6, 31.6,
55.4, 42.9, 21.6, 25.6, 26.7, 43.5), SVF_T0 = c(40, 37, 30, 28,
49, 27, 34, 35, 29, 33, 20, 18, 36, 47, 43, 39, 36, 38, 48, 31,
25, 34, 31, 33, 24, 30, 45, 20, 33, 34, 29, 42, 40, 37, 37, 25,
28, 42, 19, 32, 37, 35, 20, 43, 33, 40, 40, 42, 27, 35, 29, 39,
31, 48, 41, 27, 32, 45, 39, 40, 36, 38, 27, 32, 47, 36, 24, 40,
38, 29, 43, 47, 38, 33, 44, 39, 46, 36, 32, 38, 38, 35, 26, 43,
29, 24, 33, 30, 33, 57, 40, 37, 44, 34, 46, 41, 33, 35, 29, 37
), ADAS_CogT7 = c(16, 9.32, 21.33, 17, 8.32, 11, 14.99, 10.99,
17, 18.33, 13.32, 14.34, 8.99, 7, 11.99, 15.33, 6.99, 5.33, 12.32,
13, 21.32, 7.99, 13.33, 11.99, 17.32, 16.32, 16.33, 14.66, 18.99,
17.33, 7.99, 9.33, 10.99, NA, 12.99, 16.33, 21.66, 9, 9.34, 8.66,
8.33, 13.66, 15.66, 6.66, 10.99, 13.33, 13.33, 7.99, 11.99, 11.32,
7.33, 9.66, 6.99, NA, 15.99, 15.66, 14.66, 6.32, 7, 11, 14, 10.33,
24.66, NA, 14.99, NA, 15.99, 9.32, NA, NA, 9.99, 9.33, 7.66,
17.33, 10.32, 16, 17, 12.99, 15, 14.33, 10, 14.99, 19, 13.99,
19.33, NA, 10, 6.99, 11.66, 6.66, 14.33, 16, 8.66, 10, NA, 20,
14.99, 19.66, 26.66, 8.99), ROCF_CT7 = c(33, 23.9, 28.7, 39.1,
36.4, 29.2, 11.4, 38.1, 18.1, 28.4, 22.2, 18.6, 37, 35, 33.2,
23.6, 21.1, 30.2, 28.8, 37.1, 32.2, 33.1, 26.1, 38, 23.6, 21.4,
37.3, 36.4, 15.1, 35.7, 29.9, 31.4, 32.4, NA, 37.9, 28.2, 37.2,
31.9, 36.7, 24.5, 19.2, 23.4, 32.8, 36.2, 31.7, 24.3, 31.6, 33.1,
19.2, 30.4, 32.7, 24.4, 36.2, NA, 28.1, 31.7, 19.7, 31.6, 28.3,
27.4, 37.7, 35, 33.4, 34.4, 32.1, NA, 33.7, 31.8, NA, 34.9, 15,
20.1, 31, 35, 37.1, 29, 32.6, 31.7, 28.4, 34.4, 35.7, 35.1, 35.9,
27.4, 34, NA, 17.4, 28.9, 35.4, 33.4, 30.4, 14.7, 33.2, 26.8,
NA, 20, 33.6, 26.8, 36, 34.9), ROCF_IT7 = c(1.7, 12.1, 5.9, 12.1,
24.4, 9.8, 7.6, 24.4, 9.6, 7, 11.5, 9.7, 17.1, 14.2, 4.6, 12.1,
4.5, 18.8, 8.4, 14.6, 6.1, 17.1, 15.1, 13.1, 11.8, 12.1, 15.9,
7.6, 11.1, 5.9, 23.8, 12.4, 11.1, NA, 11, 7.8, 5.6, 6.5, 10.1,
9.9, 14.4, 17, 6.4, 27.3, 16, 14.4, 19.7, 16.2, 8.5, 8, 20.5,
15.1, 15.6, NA, 9, 8.6, 8.5, 13.2, 17.7, 14.1, 11, 9.1, 7, 16,
20.4, NA, 12.3, 16, NA, 15, 8.4, 17.9, 19.7, 11.9, 18.6, 4.7,
12.1, 17.5, 9, 5.5, 17.4, 12.1, 9.7, 9.5, 13.4, NA, 7.5, 14.5,
11, 19.5, 4.9, 11.9, 26, 12, NA, 3.9, 5.5, 6.4, 4.9, 16.2), ROCF_RT7 = c(2.4,
9.3, 6.3, 12.4, 22.6, 8.9, 7.8, 21.9, 9.8, 5.1, 10.8, 4.2, 12.3,
18.9, 2.9, 13.8, 1.2, 19.9, 4.2, 9.8, 5.9, 21.3, 12.3, 10.2,
12.1, 7.8, 6.3, 7.8, 9.8, 6.3, 16.9, 11.6, 11.3, NA, 8.9, 16.9,
3.9, 6.4, 13, 9.9, 14.9, 18.4, 3.7, 15.4, 8.8, 14.2, 14.7, 14.3,
8.8, 5.1, 21.8, 14.3, 11.4, NA, 8.7, 9.5, 8.8, 14.2, 17, 13.8,
12.8, 9.7, 5.1, 15.3, 22.8, NA, 6.4, 12.7, NA, 20.9, 7.8, 11.8,
22.4, 4.3, 17.8, 4.9, 13.8, 16.4, 7.4, 5.6, 24.8, 11.8, 9.4,
11.8, 20.3, NA, 7.1, 13.9, 12.3, 23.5, 2.6, 14.8, 15.2, 11.7,
NA, 4.3, 6.7, 5.2, 7.3, 18.4), PVF_T7 = c(38.3, 49.4, 16.7, 31.4,
60.9, 29.5, 48.4, 28.4, 17.4, 32.9, 40.7, 42.6, 42.4, 32.3, 38.5,
40.4, 20.4, 40.5, 42.6, 28.4, 27.5, 24.4, 40.4, 28.7, 5.3, 38.4,
46.7, 26.4, 37.4, 23.7, 9.8, 34.9, 55.5, NA, 38.5, 38.5, 25.5,
43.5, 36.3, 30.3, 32.7, 15.4, 21.6, 40.5, 38.7, 48.6, 47.6, 26.9,
35.7, 31.9, 47.7, 35.4, 29.5, NA, 33.6, 48.3, 27.7, 55.6, 39.5,
30.4, 36.7, 38.7, 23.9, 26.4, 28.4, NA, 18.3, 47.6, NA, 35.5,
29.7, 39.4, 44.3, 26.7, 29.4, 31.3, 44.4, 30.7, 11.5, 40.9, 37.7,
40.4, 14.5, 21.4, 14.7, NA, 29.9, 20.5, 34.4, 55.4, 32.9, 20.7,
27.6, 31.6, NA, 7.7, 25.6, 22.6, 22.7, 34.5), SVF_T7 = c(26,
48, 30, 29, 43, 25, 42, 36, 31, 27, 22, 21, 40, 46, 32, 29, 32,
38, 46, 31, 20, 36, 35, 32, 12, 28, 47, 20, 33, 30, 22, 44, 35,
NA, 36, 21, 24, 45, 25, 25, 30, 27, 34, 50, 31, 42, 34, 40, 25,
31, 41, 37, 38, NA, 38, 19, 29, 46, 40, 38, 36, 34, 31, 40, 39,
NA, 19, 40, NA, 29, 44, 43, 43, 31, 45, 28, 35, 31, 33, 36, 28,
35, 34, 34, 29, NA, 26, 30, 34, 57, 40, 36, 41, 36, NA, 22, 31,
29, 26, 31)), row.names = c(NA, -100L), class = c("tbl_df", "tbl",
"data.frame"))
>
Here the data onto the two different spreadsheets have been already merged. If you would like, you can use the present data to reckon on it.
Based on the code showed, when we do multiple pivot_longer sequentially, the rows gets expanded. If we want to do this in a single step for each key/value pair at once, an option is to_long
library(dplyr)
library(tibble)
library(sjmisc)
library(forcats)
out <- db %>%
mutate(GROUP = fct_recode(GROUP, CONTROL = "CONTROLLO")) %>%
to_long(keys = c("tests0", "tests7"),
values =c("score0", "score7"),
grep("T0$", names(.), value = TRUE),
grep("T7$", names(.), value = TRUE)) %>%
as_tibble %>%
mutate(Gender = factor(Gender, levels = c(1, 0), labels = c("M", "F")),
across(c(tests0, tests7), factor))
-output
> out
# A tibble: 600 × 9
ID GROUP Gender Age Education tests0 score0 tests7 score7
<dbl> <fct> <fct> <dbl> <dbl> <fct> <dbl> <fct> <dbl>
1 1 TRAINING M 74 18 ADAS_CogT0 14.7 ADAS_CogT7 16
2 2 TRAINING F 76 4 ADAS_CogT0 15.3 ADAS_CogT7 9.32
3 3 TRAINING M 81 8 ADAS_CogT0 17.3 ADAS_CogT7 21.3
4 4 TRAINING F 74 5 ADAS_CogT0 19 ADAS_CogT7 17
5 5 TRAINING M 69 8 ADAS_CogT0 7.66 ADAS_CogT7 8.32
6 6 TRAINING F 72 11 ADAS_CogT0 12.6 ADAS_CogT7 11
7 7 TRAINING F 75 5 ADAS_CogT0 18.7 ADAS_CogT7 15.0
8 8 TRAINING M 83 5 ADAS_CogT0 15.0 ADAS_CogT7 11.0
9 9 TRAINING F 78 4 ADAS_CogT0 18.0 ADAS_CogT7 17
10 10 TRAINING F 72 8 ADAS_CogT0 17.3 ADAS_CogT7 18.3
# … with 590 more rows

R levelplot and interpolation [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
some days ago I managed to make levelplots, with interpolation, with the following command within a script:
levelplot(jan~lon*lat,APM,main="Jan",panel=panel.levelplot.raster,interpolate=T)
I accidentaly did not keep that session, only the script, so it is not possible for me to go through the history and recover all the commands I had used.
Now, after loading latticeExtra, the same command produces an empty plot. On the other hand, leaving out the last two elements of the instruction, i.e.:
levelplot(jan~lon*lat,APM,main="Jan")
The graph is drawn.
I'd like to have interpolation at the surface, but something is going wrong with
panel=panel.levelplot.raster
the same behaviour happens when rasterVis is loaded.
I guess I am missing something... any help?
The data has the following structure:
> head(APM)
lat lon jan feb mar apr may jun jul aug sep oct nov dec
1 -18.5 10.5 29.7 28.8 25.6 25.6 26.8 29.9 35.5 46.8 35.5 27.5 27.5 27.9
2 -17.5 10.5 28.8 29.8 26.3 26.2 27.8 31.6 39.7 63.1 40.4 27.6 27.6 28.3
3 -16.5 10.5 28.7 30.0 26.9 26.8 28.6 32.1 41.1 109.4 42.8 29.7 28.9 29.4
4 -15.5 10.5 28.4 29.5 27.5 26.9 29.1 34.2 46.4 109.5 40.8 29.7 29.7 28.5
5 -14.5 10.5 28.2 29.3 27.4 27.8 27.8 42.8 60.7 104.3 49.1 29.4 28.8 28.6
6 -13.5 10.5 27.8 28.4 27.7 28.3 29.8 41.2 102.8 105.7 47.8 29.5 28.5 28.0
totalling 224 cells listed by latitude and longitude:
> dput(APM)
structure(list(lat = c(-18.5, -17.5, -16.5, -15.5, -14.5, -13.5,
-12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5,
-16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5,
-7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5,
-11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5,
-15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5,
-5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5,
-10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5,
-14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5,
-18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5,
-9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5, -14.5,
-13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5,
-17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5,
-8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5,
-12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5,
-16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5,
-7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5,
-11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5,
-15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5,
-5.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5,
-10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -18.5, -17.5, -16.5, -15.5,
-14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5,
-18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5,
-9.5, -8.5, -7.5, -6.5, -5.5), lon = c(10.5, 10.5, 10.5, 10.5,
10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 11.5,
11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5,
11.5, 11.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5,
12.5, 12.5, 12.5, 12.5, 12.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5,
13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 14.5, 14.5, 14.5,
14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5,
15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5,
15.5, 15.5, 15.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5,
16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 17.5, 17.5, 17.5, 17.5, 17.5,
17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 18.5, 18.5,
18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5,
18.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5,
19.5, 19.5, 19.5, 19.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5,
20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 21.5, 21.5, 21.5, 21.5,
21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 22.5,
22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5,
22.5, 22.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5, 23.5,
23.5, 23.5, 23.5, 23.5, 23.5, 24.5, 24.5, 24.5, 24.5, 24.5, 24.5,
24.5, 24.5, 24.5, 24.5, 24.5, 24.5, 24.5, 24.5, 25.5, 25.5, 25.5,
25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5
), jan = c(29.7, 28.8, 28.7, 28.4, 28.2, 27.8, 28, 29.4, 30.3,
32.5, 33.2, 33.5, 33.1, 34.3, 28.4, 29.4, 29.6, 29, 28.9, 28.8,
28.5, 28.9, 29.9, 31, 32.2, 32.9, 35.8, 37.4, 30, 27.4, 29.6,
30, 28.4, 30.6, 30.4, 29.9, 30.2, 31.3, 33.9, 35.5, 35.8, 34.5,
30.3, 23.8, 25.2, 23.2, 22.9, 24, 27.2, 29.3, 31.6, 32.3, 31,
31.4, 37.3, 37.4, 24.9, 25.1, 23.4, 21.2, 23.5, 22.8, 23.5, 24.5,
26, 27.7, 28.6, 33.3, 37.3, 40.7, 37.9, 38.5, 27.1, 22.5, 24.7,
23.3, 24.7, 24.8, 26.6, 27.4, 30.7, 33.9, 35.9, 37.6, 30.7, 29.5,
26.6, 24.1, 24.1, 25.4, 25.4, 25.7, 28, 28.2, 32.9, 36.3, 35.2,
40.4, 22.7, 25.5, 26.5, 24.6, 24.3, 24, 25.8, 26.7, 29.4, 31.8,
35.4, 38, 37.4, 41.3, 22.2, 23.7, 26.8, 25.8, 25.3, 24, 25.1,
26.9, 29.6, 31.8, 34.4, 35.6, 39.3, 40.1, 26.2, 26.2, 26, 25.8,
25.3, 23.2, 24.5, 25.9, 26.9, 30.8, 33.3, 38.3, 40.2, 41.2, 26.9,
26.4, 27.1, 23.1, 22.9, 24, 28.5, 26.9, 27.1, 31, 32.7, 36.6,
38, 41.6, 26.6, 27.2, 27.3, 26, 23.6, 25.8, 33.2, 33.8, 25.8,
28.1, 31.6, 34.7, 35.3, 38.5, 27.3, 28.1, 28.6, 27, 31.5, 31.8,
29.9, 27.6, 25.4, 28.4, 29.8, 32.2, 36.4, 36.8, 28.2, 26.2, 27.3,
27, 27.1, 23.7, 23.5, 25.3, 26.1, 29, 29.3, 29.3, 36.4, 35.1,
24.2, 25.1, 23.5, 23.1, 24, 24.2, 24.3, 26.7, 26.7, 26.8, 29.3,
30.4, 33.3, 33.4, 24.8, 24.3, 24.5, 24.8, 26.1, 24.6, 25, 25.3,
27.2, 26.8, 28.9, 30.5, 31.2, 33.1), feb = c(28.8, 29.8, 30,
29.5, 29.3, 28.4, 28.6, 28.7, 28.7, 28.5, 29.4, 32.2, 34.4, 37.4,
29.4, 30.5, 30.8, 30.7, 29.6, 28.3, 28, 28.4, 28.4, 29, 29.5,
31.4, 33.3, 37.1, 31.7, 26.5, 29.8, 30.4, 28.4, 28.9, 27.7, 27.8,
29.3, 30, 30.4, 31.7, 32.9, 34.7, 29.1, 23.5, 24.2, 23.3, 22.7,
23.6, 25.4, 26.4, 27.7, 28.1, 26.9, 27.5, 30.4, 31.6, 22.3, 24.7,
23.7, 21.2, 23.4, 22.5, 22.3, 21.7, 23.2, 23.3, 23.2, 27.5, 31.6,
34, 33, 33.3, 26.3, 22.5, 23.9, 24, 24.5, 23.7, 24.2, 24.5, 25.4,
27.8, 31, 33.4, 29.3, 27.5, 24.7, 23.2, 23.5, 24.7, 25.7, 25.4,
25.8, 25.6, 27.2, 30.7, 30.4, 35, 21.4, 23.3, 24.1, 24.7, 25.2,
25.2, 25.6, 25.9, 27.7, 26.9, 30.1, 31.5, 32, 34.5, 20.2, 22.4,
24.9, 25.1, 25.3, 25.5, 24.1, 25.8, 26.2, 28.4, 29.3, 30.9, 33.5,
35.6, 24.3, 24.2, 24, 24.5, 24.9, 24.7, 23.8, 25, 26.2, 27.1,
29.9, 31.3, 34.6, 34.7, 25.3, 23.6, 24.9, 22.6, 23.1, 23.1, 25.3,
26.8, 24.9, 25.1, 28.1, 30.7, 32.1, 34.2, 24, 23.4, 24.6, 25.6,
24.9, 26.4, 30.6, 31.2, 22.2, 23.9, 27.6, 30.8, 29.1, 29.7, 24,
23.6, 25.4, 25.8, 29.9, 29.5, 27.2, 26.3, 23.5, 24.5, 25.6, 27,
28.9, 28.3, 24.4, 22, 23.4, 24.9, 25.4, 22.7, 23, 23, 22.4, 23.7,
24.6, 24.9, 29.8, 28.5, 21.3, 22.8, 21.3, 21.1, 21.7, 22.8, 24.4,
23.2, 23.3, 22.4, 25.4, 26.3, 27.3, 27.1, 22.8, 22.1, 21.4, 21.9,
24.1, 24.1, 26, 23.5, 23.8, 22.5, 23, 24.9, 26.5, 28)), .Names = c("lat",
"lon", "jan", "feb"), row.names = c(NA,
-224L))
The following produces a plot in a fresh interactive session of R
APM <- results of dput(APM) from the OP
library(latticeExtra)
levelplot(jan~lon*lat,APM,main="Jan",panel=panel.levelplot.raster,interpolate=T)
or alternatively
levelplot(jan~lon*lat,APM,main="Jan",interpolate=TRUE, useRaster = TRUE)
If this is not working for you, check to see if you have the latest versions of the packages in question; for example,
R version 2.15.2 (2012-10-26)
latticeExtra_0.6-24
lattice_0.20-10
RColorBrewer_1.0-5
If the problem still is not resolved, there's something else that hasn't been mentioned in this post.

Resources