How to set a working directory in R

In R, you can set a working directory in this way (I use C:/temp as an example).

setwd("C:/temp")

If this generates an error message. suspect that you used a wrong quotation mark (this can happen when you copy and paste an example from the Internet).  Just retype using '.  You can read more about it here.

You also need to be careful about the slash.  Microsoft Windows uses backward slashes, "\", to indicate the folder structure.  R uses forward slash, "/" at least on my machine.  This may be OS-dependent, so to really determine which slash is used on your environment, try this command to know the currently working directory and see which is used:

getwd()

I got this on my personal PC ("/" is used).

"C:/Users/kaz/Documents"

Alternatively, go to File and click on Change dir... to specify the working directly.  (I don't see this option in R-Studio), so this must be specific to the regular version of R).

 

How to implement an algorithm using R

This is an example for calculating Body Mass Index using height and weight.

The first line sets the working directory in which the CSV file is located.

setwd("C:/Users/123/Documents/R")

temp <- read.csv(file="practice_data.csv",header=TRUE,sep=",")

bmi=temp$weight/temp$height

bmi

 

This is the CSV file, practice_data.csv.

Name,height,weight
John,1.6,50
Mary,1.7,55
Ed,1.5,44
David,1.3,65

Cronbach Coefficient Alpha

SAS's proc cor procedure produces two types of cronbach coefficient alpha: raw value and standardized value.

proc corr alpha data=dataname_here;
var  item1 item2 item3 item4 item5 item6 item7;
run;

The result table includes two values:

Cronbach Coefficient Alpha

Variables Alpha
--------------------------------
Raw 0.74
Standardized 0.75

Standardized version is based on standardized values of all variables included in the analysis.  If you standardize the variables yourself by creating z-score version of items and apply the same procedure, you will get the same value for both raw and standardized values.

proc standardize data=dataname_here out=dataname_here_B mean=0 std=1;
var  item1 item2 item3 item4 item5 item6 item7;
run;
proc corr alpha data=dataname_here_B;
var  item1 item2 item3 item4 item5 item6 item7;
run;

Cronbach Coefficient Alpha

Variables Alpha
--------------------------------
Raw 0.75
Standardized 0.75