Count function in R [closed] - r

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
structure(list(Date = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 5L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 3L), .Label = c("13/09/14", "14/09/14", "15/09/14",
"16/08/14", "17/08/14", "18/08/14", "23/08/14", "24/08/14", "25/08/14",
"30/08/14", "31/08/14"), class = "factor"), HomeTeam = structure(c(1L,
8L, 11L, 13L, 15L, 19L, 20L, 9L, 12L, 3L, 2L, 4L, 5L, 6L, 14L,
17L, 7L, 16L, 18L, 10L, 3L, 6L, 10L, 12L, 13L, 17L, 20L, 2L,
8L, 18L, 1L, 4L, 5L, 9L, 14L, 15L, 16L, 19L, 11L, 7L), .Label = c("Arsenal",
"Aston Villa", "Burnley", "Chelsea", "Crystal Palace", "Everton",
"Hull", "Leicester", "Liverpool", "Man City", "Man United", "Newcastle",
"QPR", "Southampton", "Stoke", "Sunderland", "Swansea", "Tottenham",
"West Brom", "West Ham"), class = "factor"), AwayTeam = structure(c(5L,
6L, 17L, 7L, 2L, 16L, 18L, 14L, 10L, 4L, 12L, 8L, 20L, 1L, 19L,
3L, 15L, 11L, 13L, 9L, 11L, 4L, 15L, 5L, 16L, 19L, 14L, 7L, 1L,
9L, 10L, 17L, 3L, 2L, 12L, 8L, 18L, 6L, 13L, 20L), .Label = c("Arsenal",
"Aston Villa", "Burnley", "Chelsea", "Crystal Palace", "Everton",
"Hull", "Leicester", "Liverpool", "Man City", "Man United", "Newcastle",
"QPR", "Southampton", "Stoke", "Sunderland", "Swansea", "Tottenham",
"West Brom", "West Ham"), class = "factor"), FTR = structure(c(3L,
2L, 1L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 2L, 3L, 1L, 2L, 2L, 3L, 2L,
2L, 3L, 3L, 2L, 1L, 1L, 2L, 3L, 3L, 1L, 3L, 2L, 1L, 2L, 3L, 2L,
1L, 3L, 1L, 2L, 1L, 3L, 2L), .Label = c("A", "D", "H"), class = "factor"),
Referee = structure(c(4L, 10L, 9L, 3L, 1L, 12L, 2L, 8L, 7L,
11L, 9L, 6L, 8L, 5L, 15L, 3L, 4L, 7L, 1L, 11L, 2L, 4L, 6L,
10L, 16L, 14L, 9L, 8L, 1L, 13L, 8L, 5L, 9L, 6L, 2L, 11L,
3L, 1L, 13L, 7L), .Label = c("A Taylor", "C Foy", "C Pawson",
"J Moss", "K Friend", "L Mason", "M Atkinson", "M Clattenburg",
"M Dean", "M Jones", "M Oliver", "N Swarbrick", "P Dowd",
"P Tierney", "R East", "R Madley"), class = "factor")), .Names = c("Date",
"HomeTeam", "AwayTeam", "FTR", "Referee"), row.names = c(NA,
40L), class = "data.frame")
In the above dataset I am trying to find out the referee who served the most number of matches for each team. For example, which guy refereed for Aston Villa the most in home games and in away games and both.
Sorry about me being blunt with my question. I did make an attempt.
In order to find out how many times referee J Moss refereed for Arsenal I tried this,
awayref<-nrow(awayref<-(filter(fd,fd$Referee=='J Moss',fd$AwayTeam=='Arsenal')))
homeref<-nrow(hf<-(filter(fd,fd$Referee=='J Moss',fd$HomeTeam=='Arsenal')))
View(total<-homeref+awayref)
I needed some help with looping it to include all referees and all teams.

We can do
tbl1 <- table(df1$Referee)
tbl1[which.max(tbl1)]

Related

Ggplot2 graph in R Shiny with line according to user input

I want to make a graph in my Shiny App using ggplot2 that plots the line regarding the user selection by topic. In this question I was told how to add those 0s in case given topic didn't have any item (using ankrun's answer). Now I'm looking for fitting this graph in a shiny App and thought about adding a sliderInput where you could choose the topic for the line. This is what I've tried:
# # # global # # #
#1. App
if("shiny" %in% rownames(installed.packages()) == FALSE){ install.packages("shiny") }
library(shiny)
#2. Easier data handling
if("dplyr" %in% rownames(installed.packages()) == FALSE){ install.packages("dplyr") }
library(dplyr)
#3. Graphs
if("ggplot2" %in% rownames(installed.packages()) == FALSE){ install.packages("ggplot2") }
library(ggplot2)
#4. Completion in graphs
if("tidyr" %in% rownames(installed.packages()) == FALSE){ install.packages("tidyr") }
library(tidyr)
# # # ui # # #
ui <- fluidPage(
sidebarPanel(
selectInput("select_topic_timeline", "What topic?",
choices = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"),
selected = c("1", "2", "3", "4","5", "6", "7", "8", "9", "10", "11", "12"),
multiple = T)
),#sidebarPanel
mainPanel(
plotOutput("per_topic_timeline")
) #mainPanel
) #fluidPage
# # # server # # #
server <- function(input, output, session) {
# TIMELINE PER TOPIC PER YEAR
output$per_topic_timeline <- renderPlot({
dtd2 <- structure(list(Topic = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("Topic 1",
"Topic 10", "Topic 11", "Topic 12", "Topic 2", "Topic 3", "Topic 4",
"Topic 5", "Topic 6", "Topic 7", "Topic 8", "Topic 9"), class = "factor"),
Year = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 2L,
3L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 6L, 7L, 8L,
9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), .Label = c("2011",
"2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019"
), class = "factor"), Count = c(3L, 3L, 3L, 5L, 5L, 11L,
17L, 14L, 4L, 1L, 1L, 4L, 2L, 3L, 9L, 4L, 2L, 1L, 3L, 4L,
5L, 18L, 23L, 19L, 15L, 1L, 5L, 6L, 8L, 11L, 17L, 7L, 1L,
3L, 6L, 4L, 20L, 21L, 18L, 12L, 3L, 1L, 1L, 2L, 5L, 5L, 11L,
5L, 2L, 1L, 1L, 2L, 2L, 5L, 7L, 23L, 9L, 1L, 1L, 2L, 3L,
6L, 4L, 9L, 8L, 1L, 1L, 6L, 2L, 3L, 3L, 1L, 3L, 2L, 5L, 7L,
11L, 11L, 28L, 11L, 2L, 1L, 2L, 2L, 5L, 6L, 5L, 16L, 3L,
4L, 2L, 2L, 7L, 6L, 8L, 6L)), row.names = c(NA, -96L), class = "data.frame")
dtd2 %>%
expand(Topic = factor(Topic, levels = gtools::mixedsort(levels(c(input$select_topic_timeline)))) ,
Year = unique(Year)) %>%
left_join(dtd2) %>%
mutate(Count = replace_na(Count, 0)) %>%
ggplot(aes(x = Year, y = Count), colour = c(input$select_topic_timeline), group = Topic) +
geom_point() +
geom_line() +
labs(x = "Year", y = NULL, title = "Timeline")
})
}
shinyApp(ui,server)
It's not printing any error out, but it's saying Warning: Column "Topic" joining factors with different levels, coercing to character vector and it's not giving any graph out, only an empty gray box. I'm not sure about how to fit the input in the graph code and I must've changed it wrong!
This issue is that you are basically refactoring the dtd2$Topic based on what's in the selectInput, but since the Topic only has 9 levels, and you can select up to 12 levels with selectInput, you are getting errors.
For this reason, I'd suggest instead using #Ronak Shah's answer with tidyr::complete instead.
Once you've completed the data, you should then dplyr::filter by Topic to get the what I believe to be the desired result:
# # # global # # #
#1. App
if("shiny" %in% rownames(installed.packages()) == FALSE){ install.packages("shiny") }
library(shiny)
#2. Easier data handling
if("dplyr" %in% rownames(installed.packages()) == FALSE){ install.packages("dplyr") }
library(dplyr)
#3. Graphs
if("ggplot2" %in% rownames(installed.packages()) == FALSE){ install.packages("ggplot2") }
library(ggplot2)
#4. Completion in graphs
if("tidyr" %in% rownames(installed.packages()) == FALSE){ install.packages("tidyr") }
library(tidyr)
# # # ui # # #
ui <- fluidPage(
sidebarPanel(
selectInput("select_topic_timeline",
label = "What topic?",
choices = as.character(1:12),
selected = as.character(1:12),
multiple = TRUE),
),#sidebarPanel
mainPanel(
plotOutput("per_topic_timeline")
) #mainPanel
) #fluidPage
# # # server # # #
server <- function(input, output, session) {
# TIMELINE PER TOPIC PER YEAR
output$per_topic_timeline <- renderPlot({
dtd2 <- structure(list(Topic = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("Topic 1",
"Topic 10", "Topic 11", "Topic 12", "Topic 2", "Topic 3", "Topic 4",
"Topic 5", "Topic 6", "Topic 7", "Topic 8", "Topic 9"), class = "factor"),
Year = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 2L,
3L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 6L, 7L, 8L,
9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), .Label = c("2011",
"2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019"
), class = "factor"), Count = c(3L, 3L, 3L, 5L, 5L, 11L,
17L, 14L, 4L, 1L, 1L, 4L, 2L, 3L, 9L, 4L, 2L, 1L, 3L, 4L,
5L, 18L, 23L, 19L, 15L, 1L, 5L, 6L, 8L, 11L, 17L, 7L, 1L,
3L, 6L, 4L, 20L, 21L, 18L, 12L, 3L, 1L, 1L, 2L, 5L, 5L, 11L,
5L, 2L, 1L, 1L, 2L, 2L, 5L, 7L, 23L, 9L, 1L, 1L, 2L, 3L,
6L, 4L, 9L, 8L, 1L, 1L, 6L, 2L, 3L, 3L, 1L, 3L, 2L, 5L, 7L,
11L, 11L, 28L, 11L, 2L, 1L, 2L, 2L, 5L, 6L, 5L, 16L, 3L,
4L, 2L, 2L, 7L, 6L, 8L, 6L)), row.names = c(NA, -96L), class = "data.frame")
dtd2 %>%
complete(Topic, Year = unique(Year), fill = list(Count = 0)) %>%
filter(Topic %in% paste("Topic", input$select_topic_timeline)) %>%
ggplot(aes(x = Year, y = Count, colour = Topic, group = Topic)) +
geom_point() +
geom_line() +
labs(x = "Year", y = NULL, title = "Timeline")
})
}
shinyApp(ui,server)
As a side note, I'd also recommend using the shinyWidgets package for this particular input, specifically shinyWidgets::pickerInput, rather than the vanilla shiny::selectInput. I'll leave that decision up to you though.

Facets: organising their order and organising the levels within facets

I would like to please organise the following plots so that facets are printed out from most to least busy (i.e. Hemiptera, Coleoptera, Hymenoptera, Siphonaptera, Lepidoptera, etc.)
I would also like to order the levels within each facet like in Coleoptera. I realise that the X-labels will change order too so I need each facet to print out its own X-label according the level order.
I have already read many threads and that's how I was able to organise Coleoptera. But now I want it to be more tidy.
This is the data (let me know if this format is ok, if not I can try another way):
structure(list(Order = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L), .Label = c("Coleoptera",
"Dermaptera", "Dictyoptera", "Diptera", "Hemiptera", "Hymenoptera",
"Lepidoptera", "Phthiraptera", "Psocoptera", "Siphonaptera",
"Thysanoptera"), class = "factor"), Nrange = structure(c(1L,
3L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L, 14L, 14L, 1L, 10L, 1L,
3L, 4L, 6L, 7L, 10L, 11L, 12L, 14L, NA, 1L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 14L, NA, 1L, 4L, 5L, 6L, 7L, 8L, 10L, 11L,
12L, 14L, 15L, NA, 1L, 2L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L,
13L, 14L, 4L, 10L, 11L, 12L, 14L, 1L, 4L, 10L, 11L, 12L, 13L,
14L, 1L, 5L, 10L, 1L, 4L, 6L, 7L, 10L, 11L, 12L, 14L), .Label = c("Africa",
"Africa, Asia", "Americas", "Asia", "Asia-Temp", "Asia-Trop",
"Australasia", "C&S America", "Cosmopolitan", "Cryptogenic",
"N America", "S America", "Trop", "Trop, SubTrop", "Unknown"), class = "factor"),
Records = c(16L, 1L, 9L, 7L, 11L, 17L, 1L, 15L, 8L, 8L, 5L,
1L, 2L, 1L, 5L, 1L, 1L, 1L, 1L, 9L, 9L, 2L, 1L, 4L, 11L,
10L, 30L, 15L, 9L, 2L, 2L, 2L, 34L, 11L, 21L, 1L, 21L, 16L,
8L, 1L, 14L, 3L, 5L, 25L, 4L, 2L, 1L, 1L, 8L, 1L, 10L, 1L,
2L, 1L, 1L, 8L, 5L, 2L, 1L, 2L, 2L, 9L, 1L, 2L, 1L, 3L, 1L,
12L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 3L,
3L, 2L)), .Names = c("Order", "Nrange", "Records"), row.names = c(NA,
-83L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "Order", drop = TRUE)
This is the reordering that I guess is affecting only Coleoptera.
xy<-x%>%
mutate(Nrange=reorder(Nrange,-Records,sum))
This is the plot:
to_plot<-xy %>%
filter(!is.na(Nrange))
ggplot(to_plot,aes(x=Nrange,y=Records,fill=Nrange))+
geom_col()+
theme(axis.text.x = element_text(angle=90, vjust=0.7), legend.position = "none") +
facet_wrap(~Order,ncol=3)+
labs(title="Insects recorded as alien-invasive to mainland Spain",
subtitle="Native ranges vs number of records",
caption="Data source: DAISIE (http://www.europe-aliens.org/)")
And this is the plot:
enter image description here
Assuming you're using the tidyverse (based on your code):
library(tidyverse)
xy <- x %>%
ungroup() %>%
mutate(
Order = fct_reorder(Order, Records, sum, .desc = TRUE)
)
xy %>%
filter(!is.na(Nrange)) %>%
ggplot() +
aes(x = Nrange, y = Records, fill = Nrange) +
geom_col() +
facet_wrap(~Order, ncol = 3)
fct_reorder comes from the forcats package, which I believe is now a part of the tidyverse.
Or, using base R, something like this:
xy <- x
record_sums <- tapply(xy$Records, xy$Order, sum)
levels(xy$Order) <- levels(xy$Order)[order(record_sums, decreasing = TRUE)]

drop from an intersection of two columns and based on a condition in a third column

I have the following data:
df <- structure(list(IDVar = 1:40, Major.sectors = structure(c(5L,
9L, 3L, 15L, 11L, 7L, 18L, 18L, 18L, 3L, 3L, 3L, 3L, 17L, 3L,
11L, 7L, 17L, 3L, 11L, 3L, 18L, 3L, 17L, 9L, 18L, 9L, 19L, 3L,
11L, 11L, 2L, 5L, 3L, 18L, 17L, 4L, 2L, 3L, 3L), .Label = c("Banks",
"Chemicals, rubber, plastics, non-metallic products", "Construction",
"Education, Health", "Food, beverages, tobacco", "Gas, Water, Electricity",
"Hotels & restaurants", "Insurance companies", "Machinery, equipment, furniture, recycling",
"Metals & metal products", "Other services", "Post & telecommunications",
"Primary sector", "Public administration & defense", "Publishing, printing",
"Textiles, wearing apparel, leather", "Transport", "Wholesale & retail trade",
"Wood, cork, paper"), class = "factor"), Region.in.country = structure(c(15L,
8L, 8L, 8L, 10L, 15L, 19L, 10L, 8L, 10L, 3L, 18L, 4L, 12L, 4L,
15L, 13L, 4L, 15L, 15L, 7L, 15L, 12L, 1L, 7L, 10L, 15L, 8L, 13L,
15L, 12L, 8L, 7L, 15L, 15L, 10L, 8L, 10L, 10L, 15L), .Label = c("Andalucia",
"Aragon", "Asturias", "Canary Islands", "Cantabria", "Castilla-La Mancha",
"Castilla y Leon", "Cataluna", "Ceuta", "Comunidad Valenciana",
"Extremadura", "Galicia", "Islas Baleares", "La Rioja", "Madrid",
"Melilla", "Murcia", "Navarra", "Pais Vasco"), class = "factor"),
EBIT.TA = c(-0.234432635519391, -0.884337466274593, -0.00446559204081373,
0.11109107677028, -0.137203773525798, -0.582114677880617,
0.0190497663203189, -3.04252763094666, 0.113157822682219,
-0.0255533180037229, 0.281767142199724, 0.0326641697396841,
-0.00879974750993553, 0.0542074697816672, -0.112104697294392,
-0.191945591325174, -0.00380586115226597, -0.0363239884169068,
-0.273949107908537, 0.435398668004486, -0.00563436099927988,
-2.75971618056051, -0.1047327709263, 0.151283793741506, -0.0373197549569126,
0.00912639083178201, -0.0386627754065697, -0.018235399636112,
-0.0118104711362467, -0.701299939137125, NA, 0.0191819361175666,
-0.0104887983706721, -0.801677105519484, -0.402194475974272,
-0.124125227730062, 0.143020458476649, -0.601186271451194,
0.0163269364787831, 5.09955167591238), EBIT.TA_l1 = c(-0.443687074746458,
-0.561864166134075, -0.0345769510044604, 0.0282541797531804,
-0.0181173929170762, 0.0147211350970115, 0.0588534950162799,
-1.14097109926961, 0.060100343733096, -0.0386426338471025,
0.049684095221329, 0.0558174150334904, 0.00214962169435867,
0.0399960114646072, 0.0402934579830171, -0.612359147433149,
-0.0115916125659674, 0.00739473610413031, 0.0174576615247567,
0.68624861825246, 0.0305807338940829, -3.88006243913616,
0.0410122725022661, -0.089491343996377, -0.215219123182103,
0.00967853324842811, -0.0336715197882038, 0.362424791356667,
0.221203934329637, -0.654387857513823, 0.0656934439915892,
0.0652005453654772, 0.0339559014267185, 0.0259085077216708,
-0.303606048856146, 0.0280113794301873, 0.109307291990628,
-0.470048555841697, -0.00157699300508027, -0.350519090107081
), EBIT.TA_l2 = c(-0.351308186716873, 0.00159428805074234,
-0.00604587147802615, 0.0761894448922952, -0.00348378141492824,
NA, 0.0346370866793768, -0.552226781084599, 0.00220031803369861,
-0.0285840972149053, 0.065316579236306, 0.4090851643341,
-0.0188362202518351, 0.0403848986306371, 0.091146090480032,
-0.0154168449752466, -0.0694803621032671, 0.0511978643139393,
-0.452924037757731, -0.0091835704914724, 0.0119918914092344,
0.0858960833880717, NA, 0.104901526886479, -0.23096183545392,
-0.0163058345980967, 0.100643431561465, 0.0527859573541712,
0.250207316117438, NA, 0.00193240515291123, 0.0624210741756767,
0.0178136227732972, -0.0321294913646274, -0.0699629484084657,
-0.00417176180400133, 0.209612573099415, 0.0285645570852926,
0.0551624216079071, 0.0172738293439595), Major.sectors.id = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 7L, 7L, 3L, 3L, 3L, 3L, 8L, 3L, 5L,
6L, 8L, 3L, 5L, 3L, 7L, 3L, 8L, 2L, 7L, 2L, 9L, 3L, 5L, 5L,
10L, 1L, 3L, 7L, 8L, 11L, 10L, 3L, 3L), Region.in.country.id = c(1L,
2L, 2L, 2L, 3L, 1L, 4L, 3L, 2L, 3L, 5L, 6L, 7L, 8L, 7L, 1L,
9L, 7L, 1L, 1L, 10L, 1L, 8L, 11L, 10L, 3L, 1L, 2L, 9L, 1L,
8L, 2L, 10L, 1L, 1L, 3L, 2L, 3L, 3L, 1L)), .Names = c("IDVar",
"Major.sectors", "Region.in.country", "EBIT.TA", "EBIT.TA_l1",
"EBIT.TA_l2", "Major.sectors.id", "Region.in.country.id"), row.names = c(NA,
40L), class = "data.frame")
I randomly generate a column of zero and ones for illustration.
x <- 40
df$x<- sample(c(0,1), replace=TRUE, size=x)
What I am trying to do is to do is to drop rows which have zero values based on a few conditons.
:If df$x == 1
and if intersect(region.id, sector.id) == 0 #i.e. there is no data
then drop
So, I want to group_by region and sector and if the intersect between both columns does not exist then drop that observation.
Consider the following image. I am basically looking to delete the intersects of the columns which has not data. So take sector.id: 1 and region.id: 5 there is no data so I want to remove it. (However my data is not grouped like the image below, its as the dput code.
I used NA for missing values in the sample x.
# get ready
set.seed(123) # set seed for reproducibility
df$x <- sample(c(NA,1), 40, replace = TRUE) # sample values
Base solution
# split by ids, check for values, bind together nonempty combinations
dfs_split <- split(df, list(df$Major.sectors.id, df$Region.in.country.id))
has_value <- sapply(dfs_split, function(df) !all(is.na(df$x)))
dfs_nonempty <- dfs_split[has_value]
res <- do.call(rbind, dfs_nonempty)
Explanation:
split divides the data into the groups you specified
sapply applies the test for non-missing values on each group
do.call helps to rbind the groups (which actually form a list)
dplyr solution
This is the cleaner option.
library(dplyr)
res <- df %>%
group_by(Major.sectors.id, Region.in.country.id) %>%
filter(!all(is.na(x)))

Ggplot2 geom_line error

I have a daaset which consists of data points over a time series for the proportion of people living in urban/rural areas for a number of countries. Sadly, not all countries have data for the same years. I have been trying to produce a simple line plot to show the different proportions of people living in different locations by year, but as each country has a different number of data points I am running into trouble.
I think this is because some of the countries only have data for a single year and using geom_line from ggplot2 throws the following error:
geom_path: Each group consist of only one observation. Do you need to
adjust the group aesthetic?
I was hoping that there would be some way to override this, or perhaps just plot a single point where a COUNTRY only has data for a single year. Does anyone know if this is possible, or indeed, if this is actually what this error means?!!?
Any help greatly appreciated!!!
Thanks
Here is my data:
structure(list(COUNTRY = structure(c(1L, 2L, 2L, 3L, 3L, 3L,
4L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, 11L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 1L,
2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, 11L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 14L,
14L, 14L, 14L, 1L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L,
9L, 9L, 10L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L,
13L, 13L, 13L, 14L, 14L, 14L, 14L, 1L, 2L, 2L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L,
8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, 11L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 1L, 2L,
2L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L,
7L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, 11L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 14L, 14L,
14L, 14L, 1L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 6L,
6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 9L,
9L, 10L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L,
13L, 13L, 14L, 14L, 14L, 14L, 1L, 2L, 2L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L,
8L, 9L, 9L, 9L, 9L, 9L, 10L, 11L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L), class = "factor", .Label = c("Comoros",
"Eritrea", "Ethiopia", "Kenya", "Lesotho", "Madagascar", "Malawi",
"Namibia", "Rwanda", "South Africa", "Swaziland", "Tanzania",
"Zambia", "Zimbabwe")), Year = structure(c(5L, 12L, 4L, 25L,
16L, 9L, 22L, 13L, 7L, 2L, 23L, 15L, 22L, 14L, 6L, 1L, 24L, 15L,
9L, 1L, 13L, 6L, 19L, 9L, 1L, 24L, 21L, 16L, 9L, 1L, 7L, 19L,
24L, 13L, 8L, 5L, 1L, 18L, 10L, 4L, 20L, 11L, 5L, 1L, 24L, 17L,
8L, 3L, 5L, 12L, 4L, 25L, 16L, 9L, 22L, 13L, 7L, 2L, 23L, 15L,
22L, 14L, 6L, 1L, 24L, 15L, 9L, 1L, 13L, 6L, 19L, 9L, 1L, 24L,
21L, 16L, 9L, 1L, 7L, 19L, 24L, 13L, 8L, 5L, 1L, 18L, 10L, 4L,
20L, 11L, 5L, 1L, 24L, 17L, 8L, 3L, 5L, 12L, 4L, 25L, 16L, 9L,
22L, 13L, 7L, 2L, 23L, 15L, 22L, 14L, 6L, 1L, 24L, 15L, 9L, 1L,
13L, 6L, 19L, 9L, 1L, 24L, 21L, 16L, 9L, 1L, 7L, 19L, 24L, 13L,
8L, 5L, 1L, 18L, 10L, 4L, 20L, 11L, 5L, 1L, 24L, 17L, 8L, 3L,
5L, 12L, 4L, 25L, 16L, 9L, 22L, 13L, 7L, 2L, 23L, 15L, 22L, 14L,
6L, 1L, 24L, 15L, 9L, 1L, 13L, 6L, 19L, 9L, 1L, 24L, 21L, 16L,
9L, 1L, 7L, 19L, 24L, 13L, 8L, 5L, 1L, 18L, 10L, 4L, 20L, 11L,
5L, 1L, 24L, 17L, 8L, 3L, 5L, 12L, 4L, 25L, 16L, 9L, 22L, 13L,
7L, 2L, 23L, 15L, 22L, 14L, 6L, 1L, 24L, 15L, 9L, 1L, 13L, 6L,
19L, 9L, 1L, 24L, 21L, 16L, 9L, 1L, 7L, 19L, 24L, 13L, 8L, 5L,
1L, 18L, 10L, 4L, 20L, 11L, 5L, 1L, 24L, 17L, 8L, 3L, 5L, 12L,
4L, 25L, 16L, 9L, 22L, 13L, 7L, 2L, 23L, 15L, 22L, 14L, 6L, 1L,
24L, 15L, 9L, 1L, 13L, 6L, 19L, 9L, 1L, 24L, 21L, 16L, 9L, 1L,
7L, 19L, 24L, 13L, 8L, 5L, 1L, 18L, 10L, 4L, 20L, 11L, 5L, 1L,
24L, 17L, 8L, 3L, 5L, 12L, 4L, 25L, 16L, 9L, 22L, 13L, 7L, 2L,
23L, 15L, 22L, 14L, 6L, 1L, 24L, 15L, 9L, 1L, 13L, 6L, 19L, 9L,
1L, 24L, 21L, 16L, 9L, 1L, 7L, 19L, 24L, 13L, 8L, 5L, 1L, 18L,
10L, 4L, 20L, 11L, 5L, 1L, 24L, 17L, 8L, 3L), class = "factor", .Label = c("1992",
"1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000",
"2000/1", "2001/2", "2002", "2003", "2003/4", "2004", "2005",
"2005/6", "2006", "2006/7", "2007", "2007/8", "2008/9", "2009",
"2010", "2011")), location = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("Urban",
"Rural", "Total", "Capital.City", "Other.Cities.towns", "Urban.Non.slum",
"Urban.Slum"), class = "factor"), percent = c(63.0434782608696,
93.8, 87, 79.5642604795185, 65.4240807416892, 63.0791092522326,
90.448386469558, 85.9419999774024, 92.7603614781794, 84.0437368780105,
89.9792286718626, 91.0916571421351, 87.1132950026762, 73.8624315865239,
60.8311005575454, 66.7, 96, 86.8, 90.6243926153181, 90.6911141749493,
90.7602286016099, 93.0377175475414, 86.073106379954, 84.253722056373,
77.8178199148702, 97.3, 91.8332260789258, 89.612164524266, 89.9070989918367,
94.9, 85.1351949905457, 94.8358752154967, 92.9, 89.656599879838,
90.2634019334124, 94.4, 91.6241263241579, 76.7337303943862, 68.4233513070184,
74.15601627144, 88.4802888646634, 85.4643913454376, 89.7457528950664,
81.3025210084024, 83.0579155525397, 71.5857386620092, 86.2324062094295,
87.687478493975, 63.5379061371841, 78.5, 40.7, 51.7763728811622,
32.2441768813334, 22.3138981723172, 83.3699691175754, 69.6742912391579,
76.0526239692028, 83.7290062290807, 77.4758329101792, 83.8081963934296,
67.5805226154664, 55.8951299980461, 41.9921451192584, 52.2, 92.5,
77.6, 82.0322170392223, 85.2850090044269, 70.8031150919282, 47.108593681531,
82.2215412952297, 78.3643348536815, 74.4253468485616, 94.8, 90.1711142192198,
85.0338348718722, 86.3134329333052, 90.4, 79.2813256726705, 90.7077549957666,
82.5, 77.7236217339155, 75.3278238729086, 77.7, 78.4592126267142,
67.1145693585691, 55.3459024734839, 57.8463881286199, 83.5604620304044,
83.9259722574938, 84.4589780509803, 73.3992444632325, 77.544833952707,
63.0503715222555, 75.6808008503601, 85.6943513045284, 63.4, 84.2,
51, 55.7151220012609, 34.9, 26.6, 85, 72.5, 79.2, 83.8, 80.3,
84.9, 69.6, 59, 46, 54, 93, 78.7, 83.2, 85.9, 76.7, 57.5, 83.8,
80.4, 75.6, 95, 90.4, 85.6, 86.9, 90.6, 82.2, 91.5, 84.5, 79.9,
78.1, 80.9, 81.2, 68.1, 56.8, 59.6, 84.9, 84.4, 86.5, 77, 79.1337842548663,
65.6, 79.1, 86.3, 68.421052631579, 96.1, 93.3, 93.461209969107,
82.2712525836501, 88.2708936990495, 87.6298001816506, 87.6386027991385,
93.1818181818183, 86.6666666666668, 88.1030398041979, 90.4761904761904,
83.4297434324662, 86.3744073211853, 83.6107223166148, 78.3, NA,
72.8, 80.952380952381, 87.5, 96.9073193030442, 99.1348508752745,
85.5297651573129, 86.4793919321843, 79.4520547945208, 98.2, 92.4613307718678,
85.4590408924955, 83.9378238341966, 92.1, 81.1594202898552, 96.0232554251852,
NA, 88.0377726639494, 83.690767555447, 93.4, 90.0349966633017,
71.2508707571865, 72, 79.4082828804656, 91.8032786885246, 84.5238095238095,
87.8787878787881, 75.6097560975609, 81.0643061692494, 68.4708412135189,
84.9056603773584, 89.5522388059702, 61.6438356164384, 91.7, 79.5,
77.0004220956012, 61.061381883032, 58.756042602018, 91.2594694272412,
85.20149612163, 92.4956062313464, 82.622382662868, 91.4036416540165,
91.6169313256523, 89.2957214499669, 67.6757501795213, 48.1479760952102,
NA, NA, 94.2, 94.3553068539161, 91.8799748693178, 89.3739230258784,
92.1418739343887, 86.4757947454868, 81.0102236379536, 77.0100025126874,
NA, 91.3720851411616, 92.2, 92.5003150086683, 97.8260869565219,
87.1461797069698, 93.5168077834096, NA, 90.1780793791367, 92.9758067301415,
94.9, 91.8829499602467, 81.749280834314, 65.1853441661798, 69.0503609949116,
87.2562445664681, 85.8298270239758, 90.6673511683335, 83.2861189801694,
84.9006282245266, 73.65452177457, 87.3075692692965, 85.5310215524833,
83.3333333333333, NA, NA, 98.5990187756088, 84.4640706359058,
NA, 93.9158337759274, 91.5744358611439, 100, NA, NA, NA, 88.7824144772468,
85.1972665683085, 89.54493171236, NA, NA, 89.8, NA, 100, 97.6261376125643,
96.3196943955923, 92.0952338262334, 87.9266080431752, 80.9429968520701,
NA, NA, 92.8, 95.2886158200472, 100, 86.4199793410402, NA, NA,
89.9001648604344, NA, NA, 91.5033109800214, 83.8918470610424,
73.9339911532972, 88.6921281548131, 94.309068022859, 85.3299585067346,
93.7362934447331, 86.5384615384618, 83.7424288707868, NA, 86.3836615391687,
88.1866796344726, 58.1081081081081, NA, NA, 75.7976468146464,
62.1289432084197, NA, 88.1488735873722, 84.2108238885019, 89.8335978405451,
NA, NA, NA, 86.9222656846515, 70.3584041024493, 70.9023609260137,
NA, NA, 85.9, NA, 89.8689917369566, 90.3864925686512, 92.628169473785,
80.9468895007753, 78.7885741638367, 75.4005791241575, NA, NA,
88.4, 87.7139456942162, 92.3809523809525, 83.7645232075473, NA,
NA, 89.567507133125, NA, NA, 91.6433898994358, 73.6225283043976,
65.9223049858496, 72.3148320483822, 86.2596215693035, 85.6224026570651,
87.4940330171337, 78.7499999999997, 81.9949404453665, NA, 84.5563115043796,
87.0190820047277)), .Names = c("COUNTRY", "Year", "location",
"percent"), row.names = c(NA, -336L), class = "data.frame")
I want to produce a simple plot with ggplot2 that is facetted by COUNTRY. I can do this fine using geom_point:
ggplot(meas_melt, aes(Year, percent, colour=location))+ geom_point() + facet_wrap(~COUNTRY)
However, if I try and produce a line plot with geom_line (ggplot(meas_melt, aes(Year, percent, colour=location))+ geom_line() + facet_wrap(~COUNTRY))
I get the following error:
geom_path: Each group consist of only one observation. Do you need to
adjust the group aesthetic?
I had thought that this could be because a couple of the countries have only one year's worth of data so I subsetted the date to remove these three countries like so:
ggplot(meas_melt, aes(Year, percent, colour=location))+ geom_line(data=meas_melt[!meas_melt$COUNTRY %in% c('Comoros','South Africa','Swaziland'),]) + facet_wrap(~COUNTRY)
However, I get the same error!
#Sven's answer is correct but fixes only part of the problem. Note how there's no plot for Comoros, South Africe, or Swaziland. This is because in your data, sometimes year is, e.g., 2006 or 2007, and sometimes it is "2006/7".
data[meas_melt$COUNTRY=="Swaziland",]
COUNTRY Year location percent
32 Swaziland 2006/7 Urban 94.83588
80 Swaziland 2006/7 Rural 90.70775
128 Swaziland 2006/7 Total 91.50000
176 Swaziland 2006/7 Capital.City 96.02326
224 Swaziland 2006/7 Other.Cities.towns 93.51681
272 Swaziland 2006/7 Urban.Non.slum NA
320 Swaziland 2006/7 Urban.Slum NA
Those countries really have only one "year" (hence, no line). More importantly, these odd year designations distort your x-axis. You can see that using the scales="free" argument to facet_wrap(...):
ggplot(meas_melt, aes(x=Year,y=percent, color=location)) +
geom_line(aes(group=location)) +facet_wrap(~COUNTRY, scales="free") +
theme(axis.text.x=element_text(angle=90, vjust=0.5, size=8),
legend.position="bottom")
Which produces this:
You have to specify aes(group = location) inside geom_line:
library(ggplot2)
ggplot(meas_melt, aes(Year, percent, colour=location)) +
geom_line(aes(group = location)) +
facet_wrap(~COUNTRY)

Showing data.frame as table or matrix in R

I want to show the following data.frame
df <- structure(list(Variety = structure(c(2L, 3L, 4L, 5L, 6L, 7L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L), .Label = c("F2022",
"F9917", "Hegari", "JS2002", "JS263", "PC1", "Sadabahar"), class = "factor"),
Priming = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 1L), .Label = c("CaCl2",
"Dry", "Hydro", "KCL", "KNO3", "NaCl", "Onfarm"), class = "factor"),
Letters = structure(c(1L, 3L, 10L, 11L, 10L, 19L, 27L, 5L,
28L, 11L, 18L, 20L, 9L, 1L, 22L, 14L, 30L, 26L, 24L, 3L,
22L, 9L, 16L, 10L, 15L, 25L, 6L, 7L, 17L, 30L, 18L, 13L,
20L, 27L, 19L, 29L, 23L, 2L, 8L, 12L, 6L, 31L, 8L, 22L, 4L,
32L, 21L, 33L, 2L), .Label = c("a", "at", "bcd", "bclq",
"bcq", "bd", "bds", "chlq", "ds", "e", "efg", "efgmnor",
"efgnor", "efgnr", "efgr", "eg", "fgmnor", "fmnor", "hijkl",
"hijkp", "hikl", "hklq", "ijkmp", "ijmop", "jmop", "mno",
"mnop", "mnor", "su", "t", "uv", "v", "w"), class = "factor")), .Names = c("Variety",
"Priming", "Letters"), row.names = c(NA, -49L), class = "data.frame")
as Table or matrix with Ordered Variety names along rows and Ordered Priming names along columns and showing Letter column in the main body of the table in R.
I could not figure out how to do this. Any help will be highly appreciated. Thanks in advance.
This should do it.
d <- d[order(d$Variety,d$Priming),]
dw <- reshape(data = d, idvar = 'Variety', timevar = 'Priming', direction = 'wide')
dw
You might want to edit the column names.
names(dw) <- gsub('Letters.', '', names(dw), fixed = TRUE)
Simple one
library(reshape2)
acast(data=df, formula=Variety~Priming)
CaCl2 Dry Hydro KCL KNO3 NaCl Onfarm
F2022 at mnop a hklq bds hijkl uv
F9917 a bcq hklq ds fgmnor su chlq
Hegari bcd mnor efgnr eg t ijkmp hklq
JS2002 e efg t e fmnor at bclq
JS263 efg fmnor mno efgr efgnor chlq v
PC1 e hijkp ijmop jmop hijkp efgmnor hikl
Sadabahar hijkl ds bcd bd mnop bd w

Resources