## 4월12일과제
Q1. 다섯명의 학생이 시험을 봤습니다. 학생 다섯명의 시험 점수를 담고 있는 변수를 만들어 출력해 보세요. 각 학생의 시험 점수는 다음과 같습니다.
80, 60, 70, 50, 90
score <- c(80,60,70,50,90)
Q2.앞 문제에서 만든 변수를 이용해서 이 학생들의 전체 평균 점수를 구해보세요.
mean(score)
Q3. 전체 평균 점수를 담고 있는 새 변수를 만들어 출력해 보세요. 앞문제를 풀 때 사용한 코드를 응용하면 됩니다.
all_mean_score <- mean(score)
all_mean_score
Q4.data.frame()과 c()를 조합해서 표의 내용을 데이터 프레임으로 만들어 출력해보세요.
a <- data.frame(제품 = c("사과","딸기","수박"),
가격 = c(1800, 1500, 3000),
판매량 = c(24,38,13))
a
Q5.앞에서 만든 데이터 프레임을 이용해서 과일 가격 평균, 판매량 평균을 구해보세요.
apply(a[,2:3], 2, mean)
Q6. seq()함수를 사용하여 date4라는 변수에 2015년 1월 1일부터 2015년 1월 31일까지 1일씩 증가하는 31개의 날짜를 입력하는 방법을 쓰세요.
s<- as.Date("2015-01-01")
d <- as.Date("2015-01-31")
date4 <- seq(s,d,by= "day")
date4
<help>
Generate Regular Sequences of Dates
Description
The method for seq for objects of class "Date" representing calendar dates.
Usage
## S3 method for class 'Date'
seq(from, to, by, length.out = NULL, along.with = NULL, ...)
Arguments
from
starting date. Required
to
end date. Optional.
by
increment of the sequence. Optional. See ‘Details’.
length.out
integer, optional. Desired length of the sequence.
along.with
take the length from the length of this argument.
...
arguments passed to or from other methods.
Details
by can be specified in several ways.
A number, taken to be in days.
A object of class difftime
A character string, containing one of "day", "week", "month", "quarter" or "year". This can optionally be preceded by a (positive or negative) integer and a space, or followed by "s".
See seq.POSIXt for the details of "month".
Value
A vector of class "Date".
See Also
Date
Examples
## first days of years
seq(as.Date("1910/1/1"), as.Date("1999/1/1"), "years")
## by month
seq(as.Date("2000/1/1"), by = "month", length.out = 12)
## quarters
seq(as.Date("2000/1/1"), as.Date("2003/1/1"), by = "quarter")
## find all 7th of the month between two dates, the last being a 7th.
st <- as.Date("1998-12-17")
en <- as.Date("2000-1-7")
ll <- seq(en, st, by = "-1 month")
rev(ll[ll > st & ll < en])
Q7. 아래 그림과 같이 vec1을 생성하세요.
vec1 <- c('사과','배','감','버섯','고구마')
vec1
위의 vec1에서 3번째 요소인 '감'을 제외하고 vec1의 값을 출력하세요.
new_vec1 <- vec1[-3]
new_vec1
Q8. 아래 그림과 같이 vec1과 vec2를 만드세요.
vec1 <- c('봄', '여름', '가을', '겨울')
vec2 <- c('봄', '여름', '늦여름', '초가을')
vec1과 vec2 내용을 모두 합친 결과를 출력하는 코드를 쓰세요.
vec3 <- c(vec1,vec2)
vec3
* union(vec1,vec2)
vec1에는 있는데 vec2에는 없는 결과를 출력하는 코드를 쓰세요.
for(i in 1:4){
if(vec1[i] == vec2[i]){}else
print(paste(vec1[i]))
}
* setdiff(vec1,vec2)
vec1과 vec2 모두 있는 결과를 출력하는 코드를 쓰세요.
for(i in 1:4){
if(vec1[i] == vec2[i]){
print(paste(vec1[i]))
}}
* intersect(vec1,vec2)
Q9. seasons 행렬을 만드세요.
(1)
seasons <- c('봄','여름','가을','겨울')
matrix(seasons, nrow = 2, ncol = 2)
(2)
b <- matrix(seasons, nrow = 2, ncol = 2, byrow = T)
b
(3)seasons행렬에서 여름과 겨울만 조회하는 방법을 쓰세요.
b[,2]
(4)seasons 행렬에 3번 행을 추가하여 seasons_2 행렬을 만드세요.
add_seasons <- c('봄','여름','가을','겨울','초봄','초가을')
seasons_2 <- matrix(add_seasons, nrow = 3, ncol = 2, byrow = T)
seasons_2
* rbind(b,c("초봄","초가을"))로 할 수 있음
(5)seasons_2변수에 열을 추가하여 seasons_3행렬을 만드세요.
add <- c("초여름","초겨울", "한겨울")
finally_seasons <- c(seasons_2,add)
seasons_3 <- matrix(finally_seasons,nrow=3,ncol=3)
seasons_3
* cbind(rbind(b,c("초봄","초가을")),c("초여름","초겨울","한겨울"))로 할 수 있음
<추가설명>
벡터데이터 제외 : 벡터명[-위치]
union(vector1,vector2) :합집합
setdiff(vector1,vector2) :차집합(순서에 따라 결과가 달라짐)
intersect(vector1,vector2) : 교집합
rbind(기존행열, 추가c(~~~)): 기존행열에 행추가
cbind(기존행열, 추가c(~~~)): 기존행열에 열추가
seq(as.Date("20220401"), as.Date("20220413"), by="day") #날짜형식 오류
seq(as.Date("2022-04-01"), as.Date("2022-04-13"), by="day")
seq(as.Date("2022/04/01"), as.Date("2022/04/13"), by="day")