my_func <- function(x,y,z){
if(length(x)!=length(y) | length(x)!=length(z)){
stop("All vectors must have same length")
}
z <- factor(z)
df <- data.frame(x,y,z)
print(table(df$z))
print(cor(x,y))
boxplot(df$x)
boxplot(df$y)
write.csv(df, "df.csv")
}
my_func <- function(x,y,z){
if( length(x)!= length(y) | length(x)!= length(z) ){
stop("must be of the same length")
}
x<-as.numeric(x)
y <- as.numeric(y)
# convert categorical variable
z <- factor(z)
tab <- table(z)
# create dataframe
df <- data.frame(x,y,z)
# frequency table
print(table(tab))
# correlation
print(cor(x,y,use="complete.obs"))
# boxplots
boxplot(df$x, main="Boxplot of x")
boxplot(df$y, main="Boxplot of y")
barplot(tab)
# save file
write.csv(df, "df.csv", row.names = FALSE)
return(df)
}
x <- c(12,15,18,21,17,19,23,25,20,16)
y <- c(5,7,6,8,5,9,10,11,7,6)
z <- c("A","B","A","C","B","A","C","B","A","C")
my_func(x,y,z)
## tab
## 3 4
## 2 1
## [1] 0.8574632



## x y z
## 1 12 5 A
## 2 15 7 B
## 3 18 6 A
## 4 21 8 C
## 5 17 5 B
## 6 19 9 A
## 7 23 10 C
## 8 25 11 B
## 9 20 7 A
## 10 16 6 C