Code works but doesn't stay when I run the next step in the code

Cannot seem to nut out a solution, I don't get any errors when I run each line of code. The code appears to work when I check it but then when I try to display it in a chart or histogram the renam hasn't worked nor does the fct_recode stick!

So new at this :frowning:

load libraries

library(readr)
library(tidyverse)
library(tidyr)
library(stringr)
library(ggplot2)
library(dplyr)
library(forcats)

read ashes.csv and load data

ashes <- read_csv("~/ashes.csv")
ashes

create ashes tibble

ashes_data <- tibble(ashes)
ashes_data

apply long format

ashes_long <- gather(ashes ,key='Innings', value = "description",4:13 )
ashes_long

extract values into new columns

ashes_match <- str_match(ashes_long$description, "Batting at number (\d+), scored (\d+) runs from (\d+) balls including 6 fours and 0 sixes.")
ashes_match

combine ashes_long and ashes_match into ashes_study then rename 3 and 4 and drop columns with NA

ashes_study <-cbind(ashes_long,ashes_match)
rename(ashes_study, scored = 3, balls = 4)
drop_na(ashes_study)
view(ashes_study)

change column type for scored and balls to integers

ashes_study$scored <- as.integer(ashes_study$scored)
ashes_study$balls <- as.integer(ashes_study$balls)

view(ashes_study)

recode factors for role and team

fct_recode(ashes_study$team,
England = "England",
England = "English",
Australia = "Australia"
)
fct_recode(ashes_study$role,
Batter = "bat",
Batter = "batting",
Batter = "batsman",
Wicketkeeper = "wicketkeeper",
Bowler = "bowl",
Bowler = "bowler",
Allrounder = "all-rounder",
Allrounder = "allrounder",
Allrounder = "all rounder"
)

Histogram

ggplot(ashes_study,aes(x = team)) + geom_bar()

combine ashes_long and ashes_match into ashes_study then rename 3 and 4 and drop columns with NA

ashes_study <-cbind(ashes_long,ashes_match)
rename(ashes_study, scored = 3, balls = 4)
drop_na(ashes_study)
view(ashes_study)

change column type for scored and balls to integers

ashes_study$scored <- as.integer(ashes_study$scored)
ashes_study$balls <- as.integer(ashes_study$balls)

view(ashes_study)

Try this. I think you didn't assign the result of ashes_study to a variable when you did it.

library(dplyr)

ashes_study <-cbind(ashes_long,ashes_match)

ashes_study <- ashes_study %>% 
    rename(ashes_study, scored = `3` , balls = `4` )
# you could also do filter out your NA values here, 

It would be good if you could provide a reproducible example though.

Many thanks William, code posted

No, can you just post the output of dput(head(ashes_study))

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

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