他的方法仍然可行?那么你有两个选择:定义新方法group\u by_uu.longitudioldata等等,教每个dplyr方法如何与你的类一起工作,或者使用基函数代替dplyr。你们班教坏习惯真是太可惜了。看来这是我唯一的选择。你的回答有效地消除了错误
他的方法仍然可行?那么你有两个选择:定义新方法group\u by_uu.longitudioldata等等,教每个dplyr方法如何与你的类一起工作,或者使用基函数代替dplyr。你们班教坏习惯真是太可惜了。看来这是我唯一的选择。你的回答有效地消除了错误,r,csv,dplyr,R,Csv,Dplyr,他的方法仍然可行?那么你有两个选择:定义新方法group\u by_uu.longitudioldata等等,教每个dplyr方法如何与你的类一起工作,或者使用基函数代替dplyr。你们班教坏习惯真是太可惜了。看来这是我唯一的选择。你的回答有效地消除了错误。 make_LD <- function(x){ structure(list(id = c(x$id), visit = c(x$visit), room = c(x$room), value =
他的方法仍然可行?那么你有两个选择:定义新方法
group\u by_uu.longitudioldata
等等,教每个dplyr方法如何与你的类一起工作,或者使用基函数代替dplyr。你们班教坏习惯真是太可惜了。看来这是我唯一的选择。你的回答有效地消除了错误。
make_LD <- function(x){
structure(list(id = c(x$id), visit = c(x$visit),
room = c(x$room), value = c(x$value), timepoint = c(x$timepoint)), class = "LongitudinalData")
}
data <- structure(list(id = c(14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L),
visit = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), room = c("bedroom", "bedroom",
"bedroom", "bedroom", "bedroom", "bedroom", "bedroom", "bedroom",
"bedroom", "bedroom", "bedroom", "bedroom", "bedroom", "bedroom",
"bedroom", "bedroom", "bedroom", "bedroom", "bedroom", "bedroom"
), value = c(6, 6, 2.75, 2.75, 2.75, 2.75, 6, 6, 2.75, 2.75,
2.75, 2.75, 2.75, 2.75, 2.75, 2.75, 2.75, 2.75, 2.75, 2.75
), timepoint = 53:72), .Names = c("id", "visit", "room",
"value", "timepoint"), class = "data.frame", row.names = c(NA,
-20L))
## Read in the data
library(readr)
library(magrittr)
library(dplyr)
source("oop_code_2.R")
## Load any other packages that you may need to execute your code
data <- read_csv("data/MIE.csv")
x <- make_LD(data)
out <- subject(x, 14)
subject <- function(x, id) UseMethod("subject")
subject.LongitudinalData <- function(x, subj){
subj_exist <- x %>%
group_by_(x$id) %>%
filter(x$id == subj)
return(subj_exist)
}
Error in UseMethod("group_by_") :
no applicable method for 'group_by_' applied to an object of class "LongitudinalData"
> datatest1 <- data %>%
+ group_by(id, visit, room) %>%
+ select(id, visit, room , value) %>%
+ filter(id == 14) %>%
+ summarise(valmean = mean(value))
> print(datatest1)
# A tibble: 6 x 4
# Groups: id, visit [?]
id visit room valmean
<int> <int> <chr> <dbl>
1 14 0 bedroom 4.786592
2 14 0 living room 2.750000
3 14 1 bedroom 3.401442
4 14 1 family room 8.426549
5 14 2 bedroom 18.583635
6 14 2 living room 22.550694
> datatest2 <- x %>%
+ group_by(id, visit, room) %>%
+ select(id, visit, room , value) %>%
+ filter(id == 14) %>%
+ summarise(valmean = mean(values))
Error in UseMethod("group_by_") :
no applicable method for 'group_by_' applied to an object of class "LongitudinalData"
> head(data)
# A tibble: 6 x 5
id visit room value timepoint
<int> <int> <chr> <dbl> <int>
1 14 0 bedroom 6.00 53
2 14 0 bedroom 6.00 54
3 14 0 bedroom 2.75 55
4 14 0 bedroom 2.75 56
5 14 0 bedroom 2.75 57
6 14 0 bedroom 2.75 58
> head(x)
$id
[1] 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14
[40] 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14
[79] 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14
$visit
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[60] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[119] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#replace you path for csv file as it is in your computer
df <- read.csv("C:/Users/username/Desktop/_257dbf6be13177cd110e3ef91b34ff67_data/data/MIE.csv", header=TRUE, sep=",", stringsAsFactors=FALSE)
make_LD <- function(x){
structure(list(id = c(x$id), visit = c(x$visit),
room = c(x$room), value = c(x$value), timepoint = c(x$timepoint)), class = "LongitudinalData")
}
subject <- function(x, id) UseMethod("subject")
subject.LongitudinalData <- function(x, subj){
subj_exist <- x %>%
group_by_(x$id) %>%
filter(x$id == subj)
return(subj_exist)
}
make_LD <- function(x){
structure(list(id = c(x$id), visit = c(x$visit),
room = c(x$room), value = c(x$value), timepoint = c(x$timepoint)),
class = c("LongitudinalData", "data.frame"))
}
make_LD <- function (x) {
class(x) <- c("LongitudinalData", class(x))
x
}
library(dplyr)
setClass('longitudinalData',
representation = representation(
id = "numeric",
visit = "numeric",
room = "character",
value = "numeric",
timepoint = 'numeric')
)
data = data.frame( id = rbinom(1000, 10, .75),
visit = sample(1:3, 1000, replace = TRUE),
room = sample(letters[1:5], 1000, replace = TRUE),
value = rnorm(1000, 50, 10),
timepoint = abs(rnorm(1000))
)
make_LD = function(data){
new("longitudinalData",
id = as.numeric(data$id),
visit = as.numeric(data$visit),
room = as.character(data$room),
value = as.numeric(data$value),
timepoint =as.numeric(data$timepoint))
}
x = make_LD (data)
print(x)
setGeneric(name = 'subject', def = function(.Object, n=1){standardGeneric('subject')})
setMethod(f='subject', signature = 'longitudinalData',
definition = function(.Object, n=1) {
if(n %in% .Object@id){
x = data.frame(as.factor(.Object@id), as.factor(.Object@visit), .Object@room, .Object@value, .Object@timepoint)
names(x) = c( 'id', 'visit', 'room', 'value', 'timepoint')
out = x[which(x$id == n),] %>% group_by(visit)
return(out)
} else { stop(paste("Subject", n, "is not available", sep = " "))}
})
subject(x, n=4) %>% summary
> subject(x, n=4) %>% summary
id visit room value timepoint
4 :12 1:2 a:2 Min. :25.04 Min. :0.02548
2 : 0 2:4 b:2 1st Qu.:44.80 1st Qu.:0.20043
3 : 0 3:6 c:1 Median :50.42 Median :0.53025
5 : 0 d:3 Mean :47.73 Mean :0.71829
6 : 0 e:4 3rd Qu.:52.44 3rd Qu.:1.13632
7 : 0 Max. :64.83 Max. :1.88971
(Other): 0