reshape daily timeseries dataset with days as variable - r

In this timeseries data frame below, the day of month is a variable. I would like to reshape this dataset from wide to long but keep the right date format.
structure(list(Year = c(1994, 1995, 1996, 1997, 1998, 1999, 2000,
2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 1994, 1995,
1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016),
Month = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), day1 = c(0,
0, 0, 0, 31, 0, 0, 0, 0, 0, 0, 0, 0, 7.4, 0, 0, 28.2, 0,
0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.4,
18.6, 0, 0, 0, 56, 2, 0, 0.4, 0, 0, 0, 0, 0), day2 = c(0,
0, 0, 0, 8.4, 0, 0, 0, 65.2, 0, 0, 0, 0, 0, 0, 0, 41, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5.2, 0, 0, 0, 0, 0, 0, 0,
6.8, 0, 0, 0, 0, 10.6, 0, 9.2, 0, 0, 0, 0, 21.6), day3 = c(0,
0, 0, 0, 0, 0, 0, 0, 132.4, 0, 0, 0, 0, 0, 0, 0, 0, 1.2,
0, 10.2, 0, 0, 1.6, 0, 0, 0, 0, 0, 0, 7.4, 0, 0, 0, 5.2,
7.8, 0, 2.6, 43.4, 0, 0, 0, 0, 2.6, 0, 0, 0, 0, 0, 0, 6.2
), day4 = c(0, 0, 0, 0, 0, 0, 15.6, 0, 34.6, 0, 0, 0, 0,
0, 0, 0, 81, 0, 0, 0, 0, 1, 0, 0, 0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 0, 0, 0, 6.2, 0, 39.4, 39.2,
0, 0, 0, 0, 0, 0, 8.6, 0, 0, 0, 0, 0.2, 0, 0, 0, 0, 0, 0,
0, 0, 2.4, 0, 0, 2.8, 0, 7.2, 23.2, 0, 0, 0, 2.4, 0.2, 0,
0, 0, 0, 9.6, 0, 0), day29 = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0.8, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA,
NA, 0, NA, NA, NA, 0.8, NA, NA, NA, 0, NA, NA, NA, 0, NA,
NA, NA, 0, NA, NA, NA, 0), day30 = c(0, 0, 0, 26.6, 0, 0,
0, 0, 0, 0, 8.2, 0, 0, 0, 0, 1.4, 0, 0.6, 12.2, 0, 4.8, 0,
0, 0, 0.6, 0, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), day31 = c(0,
102, 0, 0, 2.4, 0, 0, 0, 2.4, 0, 47, 0, 0, 0, 25, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
)), row.names = c(NA, 50L), class = "data.frame")
If I use melt function it will not consider the number of the days in the month putting NA values in the wrong dates such as 1994-02-30. I could remove the rows with NA values but I will need to be sure that my dataset there's no any NA value.
melt(Data,c("Year","Month")))
My desired output would be like this:
Data<-
Date Value
1994-01-01 0.1
1994-01-02 0
1994-01-03 12

You can get the data in long format, extract the data from column names, combine year, month and date values to create an actual date.
library(dplyr)
library(tidyr)
df %>%
pivot_longer(cols = starts_with('day'),
values_drop_na = TRUE) %>%
mutate(name = readr::parse_number(name)) %>%
unite(Date, Year, Month, name, sep = '-') %>%
mutate(Date = as.Date(Date))
# A tibble: 1,487 x 2
# Date value
# <date> <dbl>
# 1 1994-01-01 0
# 2 1994-01-02 0
# 3 1994-01-03 0
# 4 1994-01-04 0
# 5 1994-01-05 0
# 6 1994-01-06 8.6
# 7 1994-01-07 0
# 8 1994-01-08 2
# 9 1994-01-09 0
#10 1994-01-10 0
# … with 1,477 more rows

Related

R list within a dataframe calculation

I have the following dataframe df:
tile_type_index
71 17
81 8
71.1 17
81.1 8
71.2 17
71.3 17
material_balance
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
81 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
71.1 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
81.1 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
71.2 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
71.3 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
material_spend
71 0.3333333
81 0.3333333
71.1 0.3333333
81.1 0.3333333
71.2 0.3333333
71.3 0.3333333
df<-structure(list(tile_type_index = c(17L, 8L, 17L, 8L, 17L, 17L
), material_balance = list(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0)), material_spend = c(0.333333333333333,
0.333333333333333, 0.333333333333333, 0.333333333333333, 0.333333333333333,
0.333333333333333)), row.names = c("71", "81", "71.1", "81.1",
"71.2", "71.3"), class = "data.frame"
For each row of df, I want to add material_spend to the element of material_balance that has the index given tile_type_index. So I want to do something like material_balance[tile_type_index]<-material_spend but I'm not sure how to do this.
The result should look like the following:
tile_type_index
71 17
81 8
71.1 17
81.1 8
71.2 17
71.3 17
material_balance
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.33, 0, 0, 0
81 0, 0, 0, 0, 0, 0, 0, 0.33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
71.1 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.33, 0, 0, 0
81.1 0, 0, 0, 0, 0, 0, 0, 0.33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
71.2 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.33, 0, 0, 0
71.3 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.33, 0, 0, 0
material_spend
71 0.3333333
81 0.3333333
71.1 0.3333333
81.1 0.3333333
71.2 0.3333333
71.3 0.3333333
You can use dplyr for this. Normally dplyr assumes that you are operating on entire columns at once, but this is a special row-wise operation so we use the rowwise() verb. Then we can also use the replace function to replace part of a vector and return the updated value. Because you have a list column, we need to wrap the result in a list so the resulting value has a length of 1. So this should work
df %>%
rowwise() %>%
mutate(material_balance = list(replace(material_balance, tile_type_index, material_spend)))
in base R you could do:
df[[2]] <- do.call(Map, c(`[<-`, df[c(2,1,3)]))
or
df[[2]] <- Map(`[[<-`, df$material_balance, df$tile_type_index,df$material_spend)

R adding 10 to a specific element of a list in a dataframe

I have this dataframe df:
df<-structure(list(tile_type_index = c(9, 15, 20, 5, 20), tile_type = c("Flowers",
"Leather", "Outpost", "Wood 2", "Outpost"), material_on_hex = list(
c(0, 0, 0, 0, 0, 0, 0, 0, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0), c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1000,
0, 0, 0, 0, 0), c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0), c(0, 0, 0, 0, 1000, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0), c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))), row.names = c(NA,
5L), class = "data.frame")
tile_type_index tile_type
1 9 Flowers
2 15 Leather
3 20 Outpost
4 5 Wood 2
5 20 Outpost
material_on_hex
1 0, 0, 0, 0, 0, 0, 0, 0, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1000, 0, 0, 0, 0, 0
3 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
4 0, 0, 0, 0, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
5 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
I want to manipulate it in the following way: if the tile_type is "Outpost" then the row of material_on_hex should remain the same but if tile_type!="Outpost" then I want to add 10 to material_on_hex[tile_type_index]. The result should be:
tile_type_index tile_type
1 9 Flowers
2 15 Leather
3 20 Outpost
4 5 Wood 2
5 20 Outpost
material_on_hex
1 0, 0, 0, 0, 0, 0, 0, 0, 1010, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1010, 0, 0, 0, 0, 0
3 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
4 0, 0, 0, 0, 1010, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
5 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Trivial with a loop:
library(magrittr)
for (i in seq_len(nrow(df))) {
if (df$tile_type[i] == 'Outpost') next
tidx = df$tile_type_index[i]
df$material_on_hex[[i]][tidx] %<>% add(10)
}
# tile_type_index tile_type material_on_hex
# 1 9 Flowers 0, 0, 0, 0, 0, 0, 0, 0, 1010, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
# 2 15 Leather 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1010, 0, 0, 0, 0, 0
# 3 20 Outpost 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
# 4 5 Wood 2 0, 0, 0, 0, 1010, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
# 5 20 Outpost 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Could also use mapply():
df$material_on_hex = mapply(
\(x, y, z) {
if (x!='Outpost') z[y] %<>% add(10)
return(z)
},
x = df$tile_type, y = df$tile_type_index, z = df$material_on_hex,
SIMPLIFY = FALSE
)

Aggregating data from a combination of selected rows and columns

First, I have checked previous answers without finding the solution to my specific problem.
I have data on scientific papers with year of publication in one column and several columns with yes/no (1/0 binary data), whether the papers assign to various themes (in the data each given a 2-letter abbreviation). Here is a selection of the data set:
structure(list(YR = c(2016, 2015, 2015, 2015, 2015, 2015, 2015,
2015, 2014, 2014, 2014, 2014, 2014, 2014, 2013, 2013, 2012, 2012,
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2010, 2010, 2009,
2009, 2009, 2009, 2009, 2009, 2007, 2007, 2007, 2007, 1993, 1993,
1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1992,
1992, 1992, 1992, 1992, 1991, 1991, 1991, 1991, 1991, 1991, 1991,
1991, 1991, 1991, 1990, 1990, 1988, 1988, 1988, 1988, 1988, 1988,
1988, 1988, 1988, 1987, 1987, 1987, 1987, 1987, 1987, 1987, 1987,
1987, 1981, 1981, 1981, 1981, 1981, 1981, 1981, 1980, 1980, 1980,
1980, 1980, 1979, 1979), ALL = c(0, 0, 0, 1, 1, 1, 0, 1, 1, 1,
1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1,
0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0,
0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1,
0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
1, 1, 1, 0, 1), AB = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 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, 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, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0), BB = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0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 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1,
0, 0, 0)), .Names = c("YR", "ALL", "AB", "BB", "BS", "CS",
"DS", "EG", "FB", "GB", "KB", "KS", "LS", "LP", "MC", "NB", "NW",
"SB", "SH", "VM", "WH"), row.names = c(NA, -99L), class = "data.frame")
The following script:
part <- test[,c(2:21)]
col1 <- names(test[,2:21])
col2 <- data.frame(apply(part,2,sum))
pops.n <- cbind(col1,col2)
colnames(pops.n) <- c('theme','count')
gives me the following output:
theme count
ALL ALL 51
AB AB 2
BB BB 6
BS BS 11
CS CS 11
DS DS 4
EG EG 8
FB FB 10
GB GB 2
KB KB 2
KS KS 4
LS LS 4
LP LP 2
MC MC 3
NB NB 8
NW NW 2
SB SB 19
SH SH 4
VM VM 4
WH WH 24
However, I want an output that counts each theme per year, in a "long version" data table:
YR | theme | count
--------------------------
1975 WH 15
1976 WH 9
1977 WH 22
...
1975 AB 3
1976 AB 9
...
1976 SB 7
1978 SB 19
1979 SB 7
...
Grateful for any help.
Here is a method that "reshapes"the data long and then uses aggregate to sum over year-theme pairs.
newDat <- setNames(aggregate(values ~ YR + ind,
data=cbind(dat[1], stack(dat[-1])), sum),
c("YR", "theme", "count"))
Here, stack produces a two column data.frame with the counts in one column and the themes in the other. The year variable is added with cbind. This data.frame is the fed to aggregate which calculates the group level sums. setNames renames the variables of the output data.frame.
The first 10 lines are
head(newDat, 10)
YR ind values
1 1979 AB 0
2 1980 AB 0
3 1981 AB 0
4 1987 AB 1
5 1988 AB 0
6 1990 AB 0
7 1991 AB 0
8 1992 AB 0
9 1993 AB 0
10 2007 AB 0
You can also use data.table. First melt the data to a long format, and then sum the value by year and variable name.
library(data.table)
melt(setDT(df), id.vars = "YR")[, sum(value), by = .(YR, variable)]
## YR variable V1
## 1: 2016 ALL 0
## 2: 2015 ALL 4
## 3: 2014 ALL 5
## 4: 2013 ALL 0
## 5: 2012 ALL 5
## ---
## 336: 1988 WH 6
## 337: 1987 WH 2
## 338: 1981 WH 2
## 339: 1980 WH 1
## 340: 1979 WH 0
You have to use melt from reshape2 package like this:
melted_test <- melt(test, id.vars = 'YR')
Then, if you want the non-zero rows only, you can get them by:
melted_non_zero <- melted_test[which(melted_test$value != 0), ]
However, for how is your dataset defined, I can guess you're going to obtain a value column full of ones. Not a big deal!
Here is one way with tidyverse
library(tidyverse)
gather(dat, theme, count, -YR) %>%
group_by(YR , theme) %>%
summarise(count = sum(count)) %>%
arrange(theme)
# A tibble: 340 x 3
# Groups: YR [17]
# YR theme count
# <dbl> <chr> <dbl>
# 1 1979 AB 0
# 2 1980 AB 0
# 3 1981 AB 0
# 4 1987 AB 1
# 5 1988 AB 0
# 6 1990 AB 0
# 7 1991 AB 0
# 8 1992 AB 0
# 9 1993 AB 0
#10 2007 AB 0
# ... with 330 more rows
<

Counts by group and calculation/display of tables

I have information from science papers, where there are binary data on funding source (government (GVT) - yes/no), and categorical data on lead author nationality (LEAD).
This code counts the number of GTV funded papers per lead nation:
gvt.funding=aggregate(data.frame(count=sysrev$GVT),
list(value=sysrev$GVT),length)
gvt.not=format(round(((gvt_funding[1,2]/(gvt_funding[1,2]+
gvt_funding[2,2]))*100),1),nsmall=1)
gvt.yes=format(round(((gvt_funding[2,2]/(gvt_funding[1,2]+
gvt_funding[2,2]))*100),1),nsmall=1)
This code produces the following table:
gvt.not gvt.yes gvt.not(%) gvt.yes(%)
CAN 39 143 21.42857 78.571429
DEU 7 7 50.00000 50.000000
DNK 9 35 20.45455 79.545455
GBR 10 5 66.66667 33.333333
JPN 2 8 20.00000 80.000000
NOR 20 49 28.98551 71.014493
RUS 13 1 92.85714 7.142857
USA 84 104 44.68085 55.319149
TOTAL 214 373 36.45656 63.543441
I need a table aggregated pr year looking like this:
YEAR CAN-GVTyes CAN% DEU-GVTyes DEU% USA-GVTyes USA% etc.
2015 .... .. .... .. .... ..
2014 .... .. .... .. .... ..
2013 .... .. .... .. .... ..
etc
Appreciate any help. Below is an DPUT excerpt of data:
structure(list(YR = c(2015, 2015, 2015, 2015, 2015, 2015, 2015,
2015, 2015, 2015, 2015, 2015, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2013, 2013, 2013, 2013, 2013, 2013,
2013, 2013, 2013), NRAUTH = c(6, 7, 3, 22, 10, 4, 23, 4, 4, 11,
9, 6, 7, 9, 4, 6, 1, 3, 5, 4, 7, 5, 3, 2, 2, 4, 7, 4, 26, 6,
2, 4, 6, 5, 7, 5, 7, 3, 3, 2, 6, 3, 3), LEAD = structure(c(12L,
5L, 2L, 2L, 12L, 12L, 12L, 11L, 12L, 2L, 5L, 5L, 4L, 4L, 12L,
12L, 11L, 9L, 2L, 2L, 12L, 12L, 1L, 8L, 2L, 12L, 12L, 10L, 4L,
1L, 6L, 12L, 12L, 12L, 4L, 5L, 7L, 2L, 3L, 12L, 12L, 2L, 2L), .Label = c("AUS",
"CAN", "COS", "DEU", "DNK", "GBR", "GRL", "KOR", "NOR", "POL",
"RUS", "USA"), class = "factor"), CAN = c(0, 1, 1, 1, 1, 1, 1,
0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1), DNK = c(0, 1, 0,
0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0), GRL = c(0,
1, 1, 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, 0, 0, 0, 1, 0, 0, 0,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, 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
), ISSUE = 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, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0), PROT = 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, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), PA = 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), PEFF = 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("YR",
"NRAUTH", "LEAD", "CAN", "DNK", "GRL", "USA", "NOR", "RUS", "NATX",
"ALL", "AB", "BB", "BS", "CS", "DS", "EG", "FB", "GB", "KB",
"KS", "LS", "LP", "MC", "NB", "NW", "SB", "SH", "VM", "WH", "GVT",
"NGO", "COM", "ACA", "FUNDX", "FUNDNN", "POPSTAT", "POPABU",
"POPTR", "BOUND", "HARV", "CC", "HAB", "HABP", "POLL", "SHIP",
"TOUR", "BEH", "REPEC", "ZOO", "PHYS", "TEK", "HWC", "PRED",
"METH", "DIS", "ANA", "POPGEN", "EVO", "RESIMP", "ISSUE", "PROT",
"PA", "PEFF"), row.names = c(NA, -43L), class = "data.frame")

Counting frequencies of categorical parameters, total and by groups, and appending the output to existing matrices

In my data (DPUT below) I have many categorical binary parameters holding various information about scientific papers. These are all 0=absent/no or 1=present/yes. Does the study have government funding, yes or no? Is harvest discussed in the paper, yes or no? And so on, for more than 30 parameters. I also have the nationality of the lead author. One thing I am trying to do is to have a look at the total fraction of studies being funded from governments (GVT), and the same fraction per country.
This code gives a matrix on the number of studies not funded by GVT, and those funded by GVT:
gvt_funding=aggregate(data.frame(count=sysrevt$GVT),list(value=sysrevt$GVT),length)
which gives the matrix:
value count
0 32
1 66
Then I calculate the percentage of total:
gvt.not=format(round(((gvt_funding[1,2]/(gvt_funding[1,2]+
gvt_funding[2,2]))*100),1),nsmall=1)
gvt.yes=format(round(((gvt_funding[2,2]/(gvt_funding[1,2]+
gvt_funding[2,2]))*100),1),nsmall=1)
Now I want to append these percentages in a new column in the matrix, change the name of these two columns to "TOTAL"and "TOTAL%", and then to calculate the same ratios and percentages for all nations (as shown by the column "LEAD"), and append these ratios and percentages to the same matrix with matching column titles.
It is quite possible that there are several more elegant ways to do this, but this is as far as I have come...
Here's the data:
structure(list(YR = c(2015, 2015, 2015, 2015, 2015, 2015, 2015,
2015, 2015, 2015, 2015, 2015, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2013, 2013, 2013, 2013, 2013, 2013,
2013, 2013, 2013), NRAUTH = c(6, 7, 3, 22, 10, 4, 23, 4, 4, 11,
9, 6, 7, 9, 4, 6, 1, 3, 5, 4, 7, 5, 3, 2, 2, 4, 7, 4, 26, 6,
2, 4, 6, 5, 7, 5, 7, 3, 3, 2, 6, 3, 3), LEAD = structure(c(12L,
5L, 2L, 2L, 12L, 12L, 12L, 11L, 12L, 2L, 5L, 5L, 4L, 4L, 12L,
12L, 11L, 9L, 2L, 2L, 12L, 12L, 1L, 8L, 2L, 12L, 12L, 10L, 4L,
1L, 6L, 12L, 12L, 12L, 4L, 5L, 7L, 2L, 3L, 12L, 12L, 2L, 2L), .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 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("YR",
"NRAUTH", "LEAD", "CAN", "DNK", "GRL", "USA", "NOR", "RUS", "NATX",
"ALL", "AB", "BB", "BS", "CS", "DS", "EG", "FB", "GB", "KB",
"KS", "LS", "LP", "MC", "NB", "NW", "SB", "SH", "VM", "WH", "GVT",
"NGO", "COM", "ACA", "FUNDX", "FUNDNN", "POPSTAT", "POPABU",
"POPTR", "BOUND", "HARV", "CC", "HAB", "HABP", "POLL", "SHIP",
"TOUR", "BEH", "REPEC", "ZOO", "PHYS", "TEK", "HWC", "PRED",
"METH", "DIS", "ANA", "POPGEN", "EVO", "RESIMP", "ISSUE", "PROT",
"PA", "PEFF"), row.names = c(NA, -43L), class = "data.frame")
Like this?
tab <- addmargins(table(sysrevt$LEAD, sysrevt$GVT), 1)
tab <- cbind(tab, 100 * prop.table(tab, 1))
colnames(tab) <- c('gvt.not', 'gvt.yes', 'gvt.not(%)', 'gvt.yes(%)')
rownames(tab)[nrow(tab)] <- 'TOTAL'
round(tab, 2)
The output is
gvt.not gvt.yes gvt.not(%) gvt.yes(%)
AUS 1 1 50.00 50.00
CAN 1 8 11.11 88.89
COS 1 0 100.00 0.00
DEU 1 3 25.00 75.00
DNK 0 4 0.00 100.00
GBR 1 0 100.00 0.00
GRL 0 1 0.00 100.00
KOR 1 0 100.00 0.00
NOR 0 1 0.00 100.00
POL 1 0 100.00 0.00
RUS 1 1 50.00 50.00
USA 7 9 43.75 56.25
TOTAL 15 28 34.88 65.12

Resources