I am working on an assignment where I am supposed to read in a csv file, then send it to a function to be converted to a class object. I managed to read in the csv file and convert it to an object by doing this:
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")
}
A reproducible version of the input CSV file is:
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))
How I am running this code:
## 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)
Once I have done this, I use the object variable and send it to a generic function:
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)
}
When I run the code, it produces an error:
Error in UseMethod("group_by_") :
no applicable method for 'group_by_' applied to an object of class "LongitudinalData"
I have noticed the format of the csv files read in is organized into columns, where as the format of the data after I sent it to become an object has changed into fields.
Question is, what I am doin wrong? Thanks!
Edited/Added:
When I run this code on the data from the csv it works without error, as shown below. If this helps.
> 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
When done on the LongitudinalData object, it throws an error:
> 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"
It could also be from the way the data is formatted. Here's an example how the data looks before and after the data is converted to the LongitudinalData object.
> 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
Here's a link to the data: data