Commit d8d0dee7 authored by Stephen D Kachman's avatar Stephen D Kachman
Browse files

2021 Workhop

parent daab9b7b
require(tidyverse)
library(readxl)
FeedIntake <- read_excel("Example 1.xlsx",
sheet = " Feed Intake")
colnames(FeedIntake)[-(1:3)]=paste("Week",1:21)
FeedIntake
Strength <- read_excel("Example 1.xlsx",
sheet = "strength", na = c("N/A","N","N/a") ) %>%
rename(TRT=`Trt #`)
colnames(Strength)[-(1:3)]=paste("Week",c(1:8,10:13,16:21))
Strength
FeedIntake_long<-FeedIntake %>%
pivot_longer(-(Farm:TRT),names_to="Week",values_to="Feed Intake",)
Strength_long<-Strength %>%
fill(Farm:TRT) %>%
group_by(Farm,`Pen #`,TRT) %>%
mutate(Rep=row_number()) %>%
pivot_longer(-c(Farm:TRT,Rep),names_to="Week",values_to="strength") %>%
group_by(Farm,`Pen #`,TRT,Week) %>%
summarise(strength=mean(strength,na.rm=TRUE))
Combined <-FeedIntake_long %>%
full_join(Strength_long)
#' ---
#' title: "NPOOD Workshop Organizing Data for Analysis: Example 2"
#' author: "Steve Kachman"
#' date: "Summer 2019"
#' date: "Summer 2021"
#' ---
require(tidyverse)
......@@ -50,7 +50,7 @@ Treatments <- Weight %>%
Treatments
#'
#' Add treatment informaion to the FeedIntake tibble
#' Add treatment information to the FeedIntake tibble
#'
FeedIntake <- FeedIntake %>%
......@@ -64,7 +64,7 @@ FeedIntake
#'
FeedIntake_long <- FeedIntake %>%
gather(key="Week",value="Feed Intake",`2 wks`:`8 wks`) %>%
pivot_longer(`2 wks`:`8 wks`,names_to="Week",values_to="Feed Intake") %>%
mutate(Week=parse_number(Week))
FeedIntake_long
......
require(tidyverse)
library(readxl)
Example_3 <- read_excel("Example 3.xlsx",
sheet = "SAS")
View(Example_3)
PROC IMPORT OUT= WORK.INTAKEcsv
DATAFILE= "C:\Users\Kathy\Statistics Department\NPOD\Organiz
ing Data for Analysis Workshop 2021\files for participants\Example 2.csv"
DBMS=CSV REPLACE;
GETNAMES=YES;
DATAROW=2;
RUN;
PROC IMPORT OUT= WORK.intake
DATAFILE= "C:\Users\Kathy\Statistics Department\NPOD\Organiz
ing Data for Analysis Workshop 2019\Example 2.xlsx"
ing Data for Analysis Workshop 2021\files for participants\Example 2.xls
x"
DBMS=EXCEL REPLACE;
RANGE="' Feed Intake$'";
GETNAMES=YES;
......@@ -9,14 +10,3 @@ ing Data for Analysis Workshop 2019\Example 2.xlsx"
USEDATE=YES;
SCANTIME=YES;
RUN;
PROC IMPORT OUT= WORK.weight
DATAFILE= "C:\Users\Kathy\Statistics Department\NPOD\Organizing Data for Analysis Workshop 2019\Example 2.xlsx"
DBMS=EXCEL REPLACE;
RANGE="Weight$";
GETNAMES=YES;
MIXED=NO;
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;
*note that you need to change the path for the filename to the one on your;
*computer. ;
PROC IMPORT OUT= WORK.INTAKE1
DATAFILE= "C:\Users\Kathy\Statistics Department\NPOD\Organiz
ing Data for Analysis Workshop 2021\Example 1.xlsx"
DBMS=EXCEL REPLACE;
RANGE="' Feed Intake$'";
GETNAMES=YES;
MIXED=NO;
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;
*transposing by farm pen__ and trt, means that for each farm pen__;
*trt combination, that it will transpose any other variables into a column;
*in this case, the only other variables are the variables June_21_July_5 to;
*March_14__March_28. Note that the order of the intake rows are dependent;
*on the order of the columns in the dataset;
proc transpose data=intake1 out=intrans1(rename=(col1=intake));
by farm pen__ trt;
run;
*this code is to correctly assign a week number to each observation;
*and to rename the pen variable, so that it is easier to use;
*When you set a dataset by variables in the data, this tells SAS;
*that you want to keep track of which record SAS is on in the dataset;
*within the variables. We really only needed to set by farm pen__, since;
*we are using the pen variable in our if statement;
data intaketrans1;
retain week; *this tells SAS to keep the current value in week from record
to record. Otherwise, SAS would assign . to week;
drop _name_ _label_;
rename pen__=pen;*note that the renaming doesn't take place until after the
dataset has been created;
set intrans1;
by farm pen__ trt;
if first.pen__ then week=2; *if this is the first record of the current pen then
set the week =2;
else week=week+2; *if this is not the first record of the current pen then add
2 to the week variable;
output; *this tells SAS to output the record to the new intaketrans1 dataset;
run;
*note that you need to run the import intake SAS program first;
*in order to create the dataset needed;
proc transpose data=intake out=intrans(rename=(col1=intake));
by pen__ animal__;
run;
*this is to see what happens if you dont transpose by pen and animal';
proc transpose data=intake out=intransn(rename=(col1=intake));
run;
*create new variable for week;
data intaketrans;
drop _name_ _label_;
rename pen__=pen;
rename animal__=id;
set intrans;
week= substr(_label_,1,1);
week= substr(_label_,1,1); *substr function starting at the first character, pulls one character;
run;
......@@ -37,3 +42,7 @@ data intaketrt;
merge intaketrans trt;
by pen id;
run;
*example of merging without by pen id;
data intaketrtgarbage;
merge intaketrans trt;
run;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment