I'm an amateur user of R and I'm dealing with a data transformation issue.
I want to sum up rows if same ID. This is, only one row per ID will be given as output. Also, I want the rest of the columns summarised depending on the column.
For the column Year, I want to keep the maximum value found on the same ID rows. For the column Event, I want to keep Economical/Natural if only those values are found. However, in the ID 2 both values (Economical and Natural) are found. In this case, I want the summary of the column Event to have the value "Both_present". For the column Wood, I want to keep Na/0/1 depending on which value is found. However, in some IDs, 0 and 1 are found at the same time. When this happens, I would like to keep the value 1. Finally, for the Nature column, I want to mantain Biotic or Abiotic depending on which is found. Also, like in the case of the column Events, if both values are found at the same ID, I want to express this as "Both_present"
Here you can find an example data, and how I would like to have the output:
ID <- c("1", "1", "2", "2", "2", "3", "3", "3",
"4", "5", "5", "6", "6", "6")
Year <- c("2001", "2001", "2008", "2009", "2008", "2005", "2005", "2005",
"2000", "2010", "2010", "2008", "2007", "2006")
Event <- c("Economical", "Economical", "Natural", "Economical", "Natural", "Natural", "Natural", "Natural",
"Economical", "Economical", "Economical", "Natural", "Natural", "Natural")
Wood <- c("NA", "NA", "0", "1", "1", "1", "0", "0",
"1", "1", "1", "1", "1", "1")
Nature <- c("Biotic", "Abiotic", "Biotic", "Biotic", "Abiotic", "Biotic", "Biotic", "Biotic",
"Abiotic", "Abiotic", "Abiotic", "Biotic", "Biotic", "Biotic")
history <- data.frame(ID, Year, Event, Wood, Nature)
ID Year Event Wood Nature
1 1 2001 Economical NA Biotic
2 1 2001 Economical NA Abiotic
3 2 2008 Natural 0 Biotic
4 2 2009 Economical 1 Biotic
5 2 2008 Natural 1 Abiotic
6 3 2005 Natural 1 Biotic
7 3 2005 Natural 0 Biotic
8 3 2005 Natural 0 Biotic
9 4 2000 Economical 1 Abiotic
10 5 2010 Economical 1 Abiotic
11 5 2010 Economical 1 Abiotic
12 6 2008 Natural 1 Biotic
13 6 2007 Natural 1 Biotic
14 6 2006 Natural 1 Biotic
Here is how the output should look like:
ID2 Year2 Event2 Wood2 Nature2
1 1 2001 Economical NA Both_present
2 2 2009 Both_present 1 Both_present
3 3 2005 Natural 1 Biotic
4 4 2000 Economical 1 Abiotic
5 5 2010 Economical 1 Abiotic
6 6 2008 Natural 1 Biotic