DS4PS / cpp-527-fall-2020

http://ds4ps.org/cpp-527-fall-2020/
0 stars 1 forks source link

Lab 03 - preview.these #14

Open krbrick opened 3 years ago

krbrick commented 3 years ago

Hey guys- I'm going to be this guy because I can't stand not getting these labs 100%! in trying to use the preview.these code that is at the beginning of our lab, I'm trying to use my modified titles (last.titles) and newly combined df (try.combine).

try.combine <- cbind.data.frame(last.titles, d$claps, d$reading_time, d$publication, d$date, d$subtitle)
str(try.combine)
preview.these <- c("last.title", "subtitle", "claps", "reading_time", "publication", "date")
head( try.combine[preview.these] ) %>% pander()

the error I get is:

Error in `[.data.frame`(try.combine, preview.these) : undefined columns selected

any leads? thanks,

lecy commented 3 years ago

undefined columns:

last.titles
last.title

cbind.data.frame(last.titles, d$claps, d$reading_time, d$publication, d$date, d$subtitle)
c("last.title", "subtitle", "claps", "reading_time", "publication", "date")

When you see it:

lecy commented 3 years ago

What is last.titles in your code? How was it created?

I'm not entirely sure that data frame will be correct - depends on:

class( last.titles )  # should be a vector, not list
length( last.titles ) == nrow( d )  # ???
krbrick commented 3 years ago

lol thank you

krbrick commented 3 years ago
length( last.titles ) == nrow( d )

gives

length( last.titles ) == nrow( d ) [1] TRUE

try.combine <- cbind.data.frame(last.titles, d$claps, d$reading_time, d$publication, d$date, d$subtitle)
str(try.combine)
preview.these <- c("last.titles", "subtitle", "claps", "reading_time", "publication", "date")
head( try.combine[preview.these] ) %>% pander()

still gives me an undefined column error, but also gives an output showing the df including 'last.titles'

lecy commented 3 years ago

OK, I'm not sure what cbind.data.frame() is doing but the proper constructor is just data.frame().

Try:

d2 <- data.frame( last.titles, 
           claps=d$claps, 
           reading_time=d$reading_time, 
           publication=d$publication, 
           date=d$date, 
           subtitle=d$subtitle )
preview.these <- c("last.titles", "subtitle", "claps", "reading_time", "publication", "date")
head( d2[preview.these] ) 

This is assuming last.titles is a character vector and not a list. You did not provide enough code for us to know how you created last.titles, though. That could be your problem.

NOTE: if you don't rename the variables during construction then your new variables will be called d.claps instead of just claps:

claps=d$claps

That is probably making your life harder than it needs to be since you can just add your new variable back to the original data frame:

d$last.titles <- last.titles
preview.these <- c("last.titles", "subtitle", "claps", "reading_time", "publication", "date")
head( d[preview.these] ) 

Note the use of fences to separate code on the discussion board:

```r
d2 <- cbind.data.frame( last.titles, d$claps, d$reading_time, d$publication, d$date, d$subtitle)
preview.these <- c("last.titles", "subtitle", "claps", "reading_time", "publication", "date")
head( d2[preview.these] )