stargazer package producing empty summary table - r

i am having an issue using the stargazer package. i have downloaded the newest version of r (4.1.3) for mac but when I try to get a summary statistic table with the stargazer package the table comes up empty.here is what I have written in my script. i have downloaded stargazer and all other necessary packages I think.
stargazer(econ.dta[c("responses", "price", "offers", "bestoffer", "meanoffer", "name", "polite", "black", "tattoo", "white")] , type = "text",
summary.stat = c("mean", "sd", "median", "min", "max"))
data
structure(list(ad = structure(c(55, 59, 60, 65, 66, 67), label = "Ad ID", format.stata = "%8.0g"), responses = structure(c(0, 1, 2, 0, 5, 2), label = "Number of responses", format.stata = "%9.0g"), offers = structure(c(0, 1, 0, 0, 2, 1), label = "Number of offers", format.stata = "%9.0g"), bestoffer = structure(c(NA, 95, NA, NA, 90, 75), label = "Best offer", format.stata = "%9.0g"), meanoffer = structure(c(NA, 95, NA, NA, 82.5, 75), label = "Mean offer", format.stata = "%9.0g"),
name = structure(c(NA, 1, 1, NA, 0.200000002980232, 0), label = "Incl.\\ name", format.stata = "%8.0g"), polite = structure(c(NA, 1, 1, NA, 0.600000023841858, 0), label = "Polite", format.stata = "%8.0g"), price = structure(c(130, 110, 90, 110, 90, 110), label = "Asking price", format.stata = "%8.0g"), texttype = structure(c(0, 0, 0, 0, 0, 0), label = "Text series", format.stata = "%9.0g", labels = c(A = 0, B = 1, C = 2), class = c("haven_labelled", "vctrs_vctr", "double")),
black = structure(c(0, 0, 1, 0, 1, 1), label = "Black", format.stata = "%9.0g", labels = c(Other = 0, Black = 1), class = c("haven_labelled", "vctrs_vctr", "double" )), tattoo = structure(c(0, 0, 0, 0, 0, 0), label = "Tattoo", format.stata = "%9.0g", labels = c(Other = 0, Tattoo = 1), class = c("haven_labelled", "vctrs_vctr", "double" )), white = structure(c(1, 1, 0, 1, 0, 0), label = "White", format.stata = "%9.0g", labels = c(Other = 0, White = 1), class = c("haven_labelled", "vctrs_vctr", "double"))), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame" ))

There are two issues going on. The first is that some of the variables are actually of class "haven-labelled" which doesn't play nicely, so we need to "zap" the labels:
econ.dta <- haven::zap_labels(econ.dta)
The second is that it seems that the stargazer package doesn't work well with tibbles, so it needs to be converted as a data frame:
stargazer(as.data.frame(econ.dta[c("responses", "price", "offers", "bestoffer", "meanoffer", "name", "polite", "black", "tattoo", "white")]),
type = "text",
summary.stat = c("mean", "sd", "median", "min", "max"))
===============================================
Statistic Mean St. Dev. Median Min Max
-----------------------------------------------
responses 1.667 1.862 1.5 0 5
price 106.667 15.055 110 90 130
offers 0.667 0.816 0.5 0 2
bestoffer 86.667 10.408 90 75 95
meanoffer 84.167 10.104 82.500 75.000 95.000
name 0.550 0.526 0.600 0.000 1.000
polite 0.650 0.473 0.800 0.000 1.000
black 0.500 0.548 0.5 0 1
tattoo 0.000 0.000 0 0 0
white 0.500 0.548 0.5 0 1
-----------------------------------------------

Related

Converting a dataset from wide to long using pivot_longer, but an error is returned saying x is not a vector

I am trying to convert a pretty long dataset I built into long format, using the "_" as a separator and the suffixes as the years (1b is 2018 and 2 is 2020). I've built the code as follows:
GSS_ANES_long <- GSS_ANES %>%
select(!c(year_1b,year_2)) %>%
pivot_longer(
cols = -c(samptype, yearid, fileversion, panstat, anesid, version, V200001, V200017b, V200017c, V200017d, V202022, V202352, V202470, V202542, V202543, V202544, V202545, V202546, V202547, V202629, V202630),
names_sep = "_",
names_to = c(".value", "year"),
names_repair = "minimal")
Which worked on a previous version of my dataset. However, after tinkering some more with it on STATA, and running it again in R (I know this doesn't make sense, but I got used to variable creation in STATA and running models in R), it returned the following error.
Error in `vec_slice()`:
! `x` must be a vector, not `NULL`.
Run `rlang::last_error()` to see where the error occurred.
I know what this means, but I am not super sure how I can troubleshoot it, and the answers already posted here (at least the ones I found) were too specific to the data or too broad to just knowing how to pivot.
I am going to give an example of code below, taken from the first 5 rows of my dataset. The data itself contains many missing values so bear with me.
structure(list(samptype = structure(c(2016, 2016, 2016, 2016,
2016), format.stata = "%8.0g", labels = c(`sample from gss 2016` = 2016,
`sample from gss 2018` = 2018), class = c("haven_labelled", "vctrs_vctr",
"double")), yearid = structure(c(20160001, 20160002, 20160003,
20160004, 20160005), format.stata = "%12.0g"), fileversion = structure(c("GSS 2020 Panel Release 1 (May 2021)",
"GSS 2020 Panel Release 1 (May 2021)", "GSS 2020 Panel Release 1 (May 2021)",
"GSS 2020 Panel Release 1 (May 2021)", "GSS 2020 Panel Release 1 (May 2021)"
), format.stata = "%35s"), panstat = structure(c(1, 1, 0, 1,
0), format.stata = "%8.0g", labels = c(`not selected` = 0, `selected, eligible, and reinterviewed` = 1,
`selected, but not reinterviewed` = 2, `selected, but not eligible and not reinterviewed` = 3,
`selected, but not eligible and not reinterviewed because r lived outside us` = 31,
`selected, but not eligible and not reinterviewed because r was in institution` = 32,
`selected, but not eligible and not reinterviewed because r was deceased` = 33,
`selected, but not eligible and not reinterviewed because r was permanently incapacitated` = 34
), class = c("haven_labelled", "vctrs_vctr", "double")), wtssall_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "weight variable", format.stata = "%12.0g"),
wtssall_2 = structure(c(1.08500894295449, 0.542504471477243,
NA, 2.17001788590897, NA), label = "weight variable", format.stata = "%12.0g"),
wtssnr_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), label = "weight variable", format.stata = "%12.0g"),
wtssnr_2 = structure(c(1.44392875550612, 0.721964377753061,
NA, 2.88785751101224, NA), label = "weight variable", format.stata = "%12.0g"),
vstrat_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), label = "variance stratum", format.stata = "%8.0g"),
vstrat_2 = structure(c(3201, 3201, NA, 3201, NA), label = "variance stratum", format.stata = "%8.0g"),
vpsu_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), label = "variance primary sampling unit", format.stata = "%8.0g"),
vpsu_2 = structure(c(1, 1, NA, 1, NA), label = "variance primary sampling unit", format.stata = "%8.0g"),
year_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), label = "gss year for this respondent", format.stata = "%8.0g"),
year_2 = structure(c(2020, 2020, NA, 2020, NA), label = "gss year for this respondent", format.stata = "%8.0g"),
id_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), label = "respondent id number", format.stata = "%8.0g"),
id_2 = structure(c(1, 2, NA, 3, NA), label = "respondent id number", format.stata = "%8.0g"),
age_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), label = "age of respondent", format.stata = "%8.0g", labels = c(`89 or older` = 89), class = c("haven_labelled",
"vctrs_vctr", "double")), attend_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "how often r attends religious services", format.stata = "%8.0g", labels = c(never = 0,
`less than once a year` = 1, `about once or twice a year` = 2,
`several times a year` = 3, `about once a month` = 4, `2-3 times a year` = 5,
`nearly every week` = 6, `every week` = 7, `several times a week` = 8
), class = c("haven_labelled", "vctrs_vctr", "double")),
fair_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), label = "people fair or try to take advantage", format.stata = "%21.0g", labels = c(`People take advantage` = 0,
`People are fair` = 1), class = c("haven_labelled", "vctrs_vctr",
"double")), happy_1b = structure(c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), label = "general happiness", format.stata = "%8.0g", labels = c(`very happy` = 1,
`pretty happy` = 2, `not too happy` = 3), class = c("haven_labelled",
"vctrs_vctr", "double")), health_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "condition of health", format.stata = "%8.0g", labels = c(excellent = 1,
good = 2, fair = 3, poor = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), helpful_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "people helpful or looking out for selves", format.stata = "%11.0g", labels = c(`Not Helpful` = 0,
Helpful = 1), class = c("haven_labelled", "vctrs_vctr", "double"
)), marcohab_1b = structure(c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), label = "cohabitation status", format.stata = "%8.0g", labels = c(married = 1,
`not married, cohabitating partner` = 2, `not married, no cohabitating partner` = 3,
`not married, missing on cohabitating` = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), marital_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "marital status", format.stata = "%8.0g", labels = c(married = 1,
widowed = 2, divorced = 3, separated = 4, `never married` = 5
), class = c("haven_labelled", "vctrs_vctr", "double")),
realrinc_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), label = "r's income in constant $", format.stata = "%12.0g"),
region_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), label = "region of interview", format.stata = "%8.0g", labels = c(`new england` = 1,
`middle atlantic` = 2, `east north central` = 3, `west north central` = 4,
`south atlantic` = 5, `east south atlantic` = 6, `west south central` = 7,
mountain = 8, pacific = 9), class = c("haven_labelled", "vctrs_vctr",
"double")), rincome_1b = structure(c(NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), label = "respondents income", format.stata = "%8.0g", labels = c(`under $1,000` = 1,
`$1,000 to $2,999` = 2, `$3,000 to $3,999` = 3, `$4,000 to $4,999` = 4,
`$5,000 to $5,999` = 5, `$6,000 to $6,999` = 6, `$7,000 to $7,999` = 7,
`$8,000 to $9,999` = 8, `$10,000 to $14,999` = 9, `$15,000 to $19,999` = 10,
`$20,000 to $24,999` = 11, `$25,000 or more` = 12, refused = 13
), class = c("haven_labelled", "vctrs_vctr", "double")),
socbar_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), label = "spend evening at bar", format.stata = "%9.0g", labels = c(Often = 1,
Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), socfrend_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "spend evening with friends", format.stata = "%9.0g", labels = c(Often = 1,
Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), socommun_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "spend evening with neighbor", format.stata = "%9.0g", labels = c(Often = 1,
Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), socrel_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "spend evening with relatives", format.stata = "%9.0g", labels = c(Often = 1,
Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), trust_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "can people be trusted", format.stata = "%21.0g", labels = c(`Can't be too careful` = 0,
`People can be trusted` = 1), class = c("haven_labelled",
"vctrs_vctr", "double")), uscitzn_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "is r us citizen", format.stata = "%8.0g", labels = c(`a u.s. citizen` = 1,
`not a u.s. citizen` = 2, `a u.s. citizen born in puerto rico, the u.s. virgin islands, or the northern marianas islands (if volunteered)` = 3,
`born outside of the u.s. to parents who were u.s. citizens at that time (if volunteered)` = 4
), class = c("haven_labelled", "vctrs_vctr", "double")),
wwwhr_1b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), label = "www hours per week", format.stata = "%8.0g", labels = c(`0 hours` = 0,
`168 hours` = 168), class = c("haven_labelled", "vctrs_vctr",
"double")), conf2f_1b = structure(c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), label = "how many people r sees face to face", format.stata = "%8.0g", labels = c(`all or almost all of them` = 1,
`most of them` = 2, `about half of them` = 3, `some of them` = 4,
`none or almost none of them` = 5), class = c("haven_labelled",
"vctrs_vctr", "double")), conwkday_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "number of people r contacts with on a typical weekday", format.stata = "%18.0g", labels = c(`0-4 people` = 1,
`5-9 people` = 2, `10-19 people` = 3, `20-49 people` = 4,
`50 or more people` = 5, `100 or more people` = 6), class = c("haven_labelled",
"vctrs_vctr", "double")), intcntct_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "how much of r's communication is via text, mobile phone, or internet", format.stata = "%32.0g", labels = c(`Low or Mid-Level Online Presence` = 0,
`High Online Presence` = 1), class = c("haven_labelled",
"vctrs_vctr", "double")), lonely1_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "how often in the past 4 weeks r has felt they lack companionship", format.stata = "%8.0g", labels = c(iap = NA_real_,
`not available for this version of the data file` = NA_real_,
`not available for this year` = NA_real_), class = c("haven_labelled",
"vctrs_vctr", "double")), lonely2_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "how often in the past 4 weeks r has felt isolated from others", format.stata = "%8.0g", labels = c(iap = NA_real_,
`not available for this version of the data file` = NA_real_,
`not available for this year` = NA_real_), class = c("haven_labelled",
"vctrs_vctr", "double")), lonely3_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "how often in the past 4 weeks r has felt left out", format.stata = "%8.0g", labels = c(never = 1,
rarely = 2, sometimes = 3, often = 4, `very often` = 5), class = c("haven_labelled",
"vctrs_vctr", "double")), partpartonline_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "in past 12 months, r has participated in orgs for politics or political associat", format.stata = "%16.0g", labels = c(`Not Participated` = 0,
Participated = 1), class = c("haven_labelled", "vctrs_vctr",
"double")), partvol_1b = structure(c(NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), label = "in past 12 months, r has participated in charitable or religious volunteer orgs", format.stata = "%16.0g", labels = c(`Not Participated` = 0,
Participated = 1), class = c("haven_labelled", "vctrs_vctr",
"double")), age_2 = structure(c(51, 65, NA, 47, NA), label = "age of respondent", format.stata = "%8.0g", labels = c(`89 or older` = 89), class = c("haven_labelled",
"vctrs_vctr", "double")), attend_2 = structure(c(1, 1, NA,
4, NA), label = "how often r attends religious services", format.stata = "%8.0g", labels = c(never = 0,
`less than once a year` = 1, `about once or twice a year` = 2,
`several times a year` = 3, `about once a month` = 4, `2-3 times a year` = 5,
`nearly every week` = 6, `every week` = 7, `several times a week` = 8
), class = c("haven_labelled", "vctrs_vctr", "double")),
fair_2 = structure(c(NA, 1, NA, NA, NA), label = "people fair or try to take advantage", format.stata = "%21.0g", labels = c(`People take advantage` = 0,
`People are fair` = 1), class = c("haven_labelled", "vctrs_vctr",
"double")), happy_2 = structure(c(2, 2, NA, 2, NA), label = "general happiness", format.stata = "%8.0g", labels = c(`very happy` = 1,
`pretty happy` = 2, `not too happy` = 3), class = c("haven_labelled",
"vctrs_vctr", "double")), health_2 = structure(c(3, NA, NA,
2, NA), label = "condition of health", format.stata = "%8.0g", labels = c(excellent = 1,
good = 2, fair = 3, poor = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), helpful_2 = structure(c(NA, 0,
NA, NA, NA), label = "people helpful or looking out for selves", format.stata = "%11.0g", labels = c(`Not Helpful` = 0,
Helpful = 1), class = c("haven_labelled", "vctrs_vctr", "double"
)), marcohab_2 = structure(c(1, 3, NA, 1, NA), label = "cohabitation status", format.stata = "%8.0g", labels = c(married = 1,
`not married, cohabitating partner` = 2, `not married, no cohabitating partner` = 3,
`not married, missing on cohabitating` = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), marital_2 = structure(c(1, 5, NA,
1, NA), label = "marital status", format.stata = "%8.0g", labels = c(married = 1,
widowed = 2, divorced = 3, separated = 4, `never married` = 5
), class = c("haven_labelled", "vctrs_vctr", "double")),
realrinc_2 = structure(c(147659.41804, 23980, NA, NA, NA), label = "r's income in constant $", format.stata = "%12.0g"),
region_2 = structure(c(1, 1, NA, 1, NA), label = "region of interview", format.stata = "%8.0g", labels = c(`new england` = 1,
`middle atlantic` = 2, `east north central` = 3, `west north central` = 4,
`south atlantic` = 5, `east south atlantic` = 6, `west south central` = 7,
mountain = 8, pacific = 9), class = c("haven_labelled", "vctrs_vctr",
"double")), rincome_2 = structure(c(13, 12, NA, NA, NA), label = "respondents income", format.stata = "%8.0g", labels = c(`under $1,000` = 1,
`$1,000 to $2,999` = 2, `$3,000 to $3,999` = 3, `$4,000 to $4,999` = 4,
`$5,000 to $5,999` = 5, `$6,000 to $6,999` = 6, `$7,000 to $7,999` = 7,
`$8,000 to $9,999` = 8, `$10,000 to $14,999` = 9, `$15,000 to $19,999` = 10,
`$20,000 to $24,999` = 11, `$25,000 or more` = 12, refused = 13
), class = c("haven_labelled", "vctrs_vctr", "double")),
socbar_2 = structure(c(3, 4, NA, 2, NA), label = "spend evening at bar", format.stata = "%9.0g", labels = c(Often = 1,
Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), socfrend_2 = structure(c(3, 3,
NA, 2, NA), label = "spend evening with friends", format.stata = "%9.0g", labels = c(Often = 1,
Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), socommun_2 = structure(c(1, 1,
NA, 3, NA), label = "spend evening with neighbor", format.stata = "%9.0g", labels = c(Often = 1,
Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), socrel_2 = structure(c(3, 3, NA,
3, NA), label = "spend evening with relatives", format.stata = "%9.0g", labels = c(Often = 1,
Sometimes = 2, Rarely = 3, Never = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), trust_2 = structure(c(NA, 1, NA,
NA, NA), label = "can people be trusted", format.stata = "%21.0g", labels = c(`Can't be too careful` = 0,
`People can be trusted` = 1), class = c("haven_labelled",
"vctrs_vctr", "double")), uscitzn_2 = structure(c(1, 1, NA,
1, NA), label = "is r us citizen", format.stata = "%8.0g", labels = c(`a u.s. citizen` = 1,
`not a u.s. citizen` = 2, `a u.s. citizen born in puerto rico, the u.s. virgin islands, or the northern marianas islands (if volunteered)` = 3,
`born outside of the u.s. to parents who were u.s. citizens at that time (if volunteered)` = 4
), class = c("haven_labelled", "vctrs_vctr", "double")),
wwwhr_2 = structure(c(20, 10, NA, 2, NA), label = "www hours per week", format.stata = "%8.0g", labels = c(`0 hours` = 0,
`168 hours` = 168), class = c("haven_labelled", "vctrs_vctr",
"double")), conf2f_2 = structure(c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), label = "how many people r sees face to face", format.stata = "%8.0g", labels = c(iap = NA_real_,
`not available for this version of the data file` = NA_real_,
`not available for this year` = NA_real_), class = c("haven_labelled",
"vctrs_vctr", "double")), conwkday_2 = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "number of people r contacts with on a typical weekday", format.stata = "%8.0g", labels = c(iap = NA_real_,
`not available for this version of the data file` = NA_real_,
`not available for this year` = NA_real_), class = c("haven_labelled",
"vctrs_vctr", "double")), lonely1_2 = structure(c(2, 3, NA,
1, NA), label = "how often in the past 4 weeks r has felt they lack companionship", format.stata = "%8.0g", labels = c(never = 1,
rarely = 2, sometimes = 3, often = 4, `very often` = 5), class = c("haven_labelled",
"vctrs_vctr", "double")), lonely2_2 = structure(c(1, 1, NA,
3, NA), label = "how often in the past 4 weeks r has felt isolated from others", format.stata = "%8.0g", labels = c(never = 1,
rarely = 2, sometimes = 3, often = 4, `very often` = 5), class = c("haven_labelled",
"vctrs_vctr", "double")), lonely3_2 = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "how often in the past 4 weeks r has felt left out", format.stata = "%8.0g", labels = c(iap = NA_real_,
`not available for this version of the data file` = NA_real_,
`not available for this year` = NA_real_), class = c("haven_labelled",
"vctrs_vctr", "double")), anesid = structure(c(169657, 169664,
NA, NA, NA), format.stata = "%10.0g"), version = structure(c("ANES-GSS_2020JointStudy_20220408",
"ANES-GSS_2020JointStudy_20220408", "", "", ""), label = "Version of ANES-GSS 2020 Joint Study Release", format.stata = "%32s"),
V200001 = structure(c(169657, 169664, NA, NA, NA), label = "2020 Case ID", format.stata = "%12.0g"),
V200017b = structure(c(1.40270924414924, 1.58917078954157,
NA, NA, NA), label = "GSS sample post-election weight", format.stata = "%12.0g"),
V200017c = structure(c(1, 1, NA, NA, NA), label = "GSS sample variance unit", format.stata = "%12.0g"),
V200017d = structure(c(1, 1, NA, NA, NA), label = "GSS sample variance stratum", format.stata = "%12.0g"),
V202022 = structure(c(1, 1, NA, NA, NA), label = "POST: R ever discuss politics with family or friends", format.stata = "%12.0g", labels = c(`-9. Refused` = -9,
`1. Yes` = 1, `2. No` = 2), class = c("haven_labelled", "vctrs_vctr",
"double")), V202352 = structure(c(4, 2, NA, NA, NA), label = "POST: How would R describe social class [EGSS]", format.stata = "%12.0g", labels = c(`-9. Refused` = -9,
`-8. Don't know` = -8, `-5. Interview breakoff (sufficient partial IW)` = -5,
`1. Lower class` = 1, `2. Working class` = 2, `3. Middle class` = 3,
`4. Upper class` = 4), class = c("haven_labelled", "vctrs_vctr",
"double")), V202470 = structure(c(3, 3, NA, NA, NA), label = "POST: R currently smoking", format.stata = "%12.0g", labels = c(`-9. Refused` = -9,
`-5. Interview breakoff (sufficient partial IW)` = -5, `-1. Inapplicable` = -1,
`1. Every day` = 1, `2. Some days` = 2, `3. Not at all` = 3
), class = c("haven_labelled", "vctrs_vctr", "double")),
V202542 = structure(c(3, NA, NA, NA, NA), label = "POST: How often use Facebook", format.stata = "%12.0g", labels = c(`-9. Refused` = -9,
`-5. Interview breakoff (sufficient partial IW)` = -5, `-1. Inapplicable` = -1,
`1. Many times every day` = 1, `2. A few times every day` = 2,
`3. About once a day` = 3, `4. A few times each week` = 4,
`5. About once a week` = 5, `6. Once or twice a month` = 6,
`7. Less than once a month` = 7), class = c("haven_labelled",
"vctrs_vctr", "double")), V202543 = structure(c(5, NA, NA,
NA, NA), label = "POST: How often post political content on Facebook", format.stata = "%12.0g", labels = c(`-9. Refused` = -9,
`-5. Interview breakoff (sufficient partial IW)` = -5, `-1. Inapplicable` = -1,
`1. Always` = 1, `2. Most of the time` = 2, `3. About half of the time` = 3,
`4. Sometimes` = 4, `5. Never` = 5), class = c("haven_labelled",
"vctrs_vctr", "double")), V202544 = structure(c(6, NA, NA,
NA, NA), label = "POST: How often use Twitter", format.stata = "%12.0g", labels = c(`-9. Refused` = -9,
`-5. Interview breakoff (sufficient partial IW)` = -5, `-1. Inapplicable` = -1,
`1. Many times every day` = 1, `2. A few times every day` = 2,
`3. About once a day` = 3, `4. A few times each week` = 4,
`5. About once a week` = 5, `6. Once or twice a month` = 6,
`7. Less than once a month` = 7), class = c("haven_labelled",
"vctrs_vctr", "double")), V202545 = structure(c(5, NA, NA,
NA, NA), label = "POST: How often post political content on Twitter", format.stata = "%12.0g", labels = c(`-9. Refused` = -9,
`-5. Interview breakoff (sufficient partial IW)` = -5, `-1. Inapplicable` = -1,
`1. Always` = 1, `2. Most of the time` = 2, `3. About half of the time` = 3,
`4. Sometimes` = 4, `5. Never` = 5), class = c("haven_labelled",
"vctrs_vctr", "double")), V202546 = structure(c(2, NA, NA,
NA, NA), label = "POST: How often use Reddit", format.stata = "%12.0g", labels = c(`-5. Interview breakoff (sufficient partial IW)` = -5,
`-1. Inapplicable` = -1, `1. Many times every day` = 1, `2. A few times every day` = 2,
`3. About once a day` = 3, `4. A few times each week` = 4,
`5. About once a week` = 5, `6. Once or twice a month` = 6,
`7. Less than once a month` = 7), class = c("haven_labelled",
"vctrs_vctr", "double")), V202547 = structure(c(5, NA, NA,
NA, NA), label = "POST: How often post political content on Reddit", format.stata = "%12.0g", labels = c(`-9. Refused` = -9,
`-5. Interview breakoff (sufficient partial IW)` = -5, `-1. Inapplicable` = -1,
`1. Always` = 1, `2. Most of the time` = 2, `3. About half of the time` = 3,
`4. Sometimes` = 4, `5. Never` = 5), class = c("haven_labelled",
"vctrs_vctr", "double")), V202629 = structure(c(1, 3, NA,
NA, NA), label = "POST: GSS: In past seven days has R been bothered by emotional problems", format.stata = "%12.0g", labels = c(`-9. Refused` = -9,
`-5. Interview breakoff (sufficient partial IW)` = -5, `1. Never` = 1,
`2. Rarely` = 2, `3. Sometimes` = 3, `4. Often` = 4, `5. Always` = 5
), class = c("haven_labelled", "vctrs_vctr", "double")),
V202630 = structure(c(3, 3, NA, NA, NA), label = "POST: GSS: Taken all together how happy is R these days", format.stata = "%12.0g", labels = c(`-9. Refused` = -9,
`-5. Interview breakoff (sufficient partial IW)` = -5, `1. Very happy` = 1,
`2. Pretty happy` = 2, `3. Not too happy` = 3), class = c("haven_labelled",
"vctrs_vctr", "double")), `_merge` = structure(c(3, 3, 1,
1, 1), label = "Matching result from merge", format.stata = "%23.0g", labels = c(`Master only (1)` = 1,
`Using only (2)` = 2, `Matched (3)` = 3, `Missing updated (4)` = 4,
`Nonmissing conflict (5)` = 5), class = c("haven_labelled",
"vctrs_vctr", "double")), agecat_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "Age in Categories", format.stata = "%9.0g", labels = c(`18-25` = 1,
`26-45` = 2, `46-64` = 3, `65+` = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), agecat_2 = structure(c(3, 4, NA,
3, NA), label = "Age in Categories", format.stata = "%9.0g", labels = c(`18-25` = 1,
`26-45` = 2, `46-64` = 3, `65+` = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), region4_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "region of interview (4 regions)", format.stata = "%10.0g", labels = c(`North-East` = 1,
Midwest = 2, South = 3, West = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), region4_2 = structure(c(1, 1, NA,
1, NA), label = "region of interview (4 regions)", format.stata = "%10.0g", labels = c(`North-East` = 1,
Midwest = 2, South = 3, West = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), attend4_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "frequency of religious attendance", format.stata = "%9.0g", labels = c(Never = 1,
Rarely = 2, Sometimes = 3, Often = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), attend4_2 = structure(c(2, 2, NA,
3, NA), label = "frequency of religious attendance", format.stata = "%9.0g", labels = c(Never = 1,
Rarely = 2, Sometimes = 3, Often = 4), class = c("haven_labelled",
"vctrs_vctr", "double")), lonely_1b = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), label = "Loneliness Scale (Physical and Emotional)", format.stata = "%28.0g", labels = c(Rarely = 1,
Sometimes = 2, Often = 3), class = c("haven_labelled", "vctrs_vctr",
"double")), lonely_2 = structure(c(1, 1, NA, 1, NA), label = "Loneliness Scale (Physical and Emotional)", format.stata = "%21.0g", labels = c(Rarely = 1,
Sometimes = 2, Often = 3), class = c("haven_labelled", "vctrs_vctr",
"double")), cohesion_1b = structure(c(NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), label = "Social Cohesion Index based on Fair, Helpful, and Trust", format.stata = "%55.0g", labels = c(`Not Fair, Not Helpful, Not Trustworthy` = 1,
`At least two No` = 2, `At least two Yes` = 3, `Fair, Helpful, and Trustworthy` = 4
), class = c("haven_labelled", "vctrs_vctr", "double")),
cohesion_2 = structure(c(NA, 3, NA, NA, NA), label = "Social Cohesion Index based on Fair, Helpful, and Trust", format.stata = "%55.0g", labels = c(`Not Fair, Not Helpful, Not Trustworthy` = 1,
`At least two No` = 2, `At least two Yes` = 3, `Fair, Helpful, and Trustworthy` = 4
), class = c("haven_labelled", "vctrs_vctr", "double")),
partpartoffline_1b = structure(c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), label = "past 12 months, r has participated in orgs for politics or political assoc.", format.stata = "%16.0g", labels = c(`Not Participated` = 0,
Participated = 1), class = c("haven_labelled", "vctrs_vctr",
"double")), partpartoffline_2 = structure(c(0, 0, NA, NA,
NA), label = "past 12 months, r has participated in political activities or orgs offline", format.stata = "%184.0g", labels = c(`Not Participated` = 0,
Participated = 1), class = c("haven_labelled", "vctrs_vctr",
"double")), partpartonline_2 = structure(c(0, 0, NA, NA,
NA), label = "past 12 months, r has participated in political activities or orgs online", format.stata = "%182.0g", labels = c(`Not Participated` = 0,
Participated = 1), class = c("haven_labelled", "vctrs_vctr",
"double")), partvol_2 = structure(c(0, 0, NA, NA, NA), label = "in past 12 months, r has participated in charitable or religious volunteer orgs", format.stata = "%88.0g", labels = c(`Not Participated` = 0,
Participated = 1), class = c("haven_labelled", "vctrs_vctr",
"double")), intcntct_2 = structure(c(1, 0, NA, NA, NA), label = "how much of r's communication is via text, mobile phone, or internet", format.stata = "%148.0g", labels = c(`Low or Mid-Level Online Presence` = 0,
`High Online Presence` = 1), class = c("haven_labelled",
"vctrs_vctr", "double"))), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
I have tried going over the variables again both in STATA and in R to verify if I had made a mistake somewhere, but I've yet to find anything solving this issue. I'm not specifically asking for a solution but even just pointing at the direction of where I might start looking for mistakes will be incredibly useful.
Edit: I thought it would be useful to share that I have a new error being:
Error:
! Column 33 must be named.
Use .name_repair to specify repair.
Caused by error in `repaired_names()`:
! Names can't be empty.
✖ Empty name found at location 33.
You have a column named _merge, that is causing the issue. Rename it or exclude it in col and the pivot will work.
Edit:
In this piece of code, any columns that start with _ like _merge are removed from the frame. Then only those that contain an underscore are used for the pivot.
GSS_ANES %>%
select(!c(year_1b,year_2, starts_with("_"))) %>% # Remove any cols starting with an underscore
pivot_longer(
cols = contains("_"), # Only use cols containing the separator for the pivot
names_sep = "_",
names_to = c(".value", "year"),
names_repair = "minimal"
)

How to use ggplot with Haven dataset

New to coding and R but have a STATA dataset, I want to use ggplot for visulations of my data however, I get multiple errors such as
no applicable method for 'rescale' applied to an object of class "c('haven_labelled', 'vctrs_vctr', 'double')"
I dont know how to convert them so I can plot them for visualisations,
the lines of code are as followed:
Data <- read_dta("longitudinal_td.dta")
Data <- Data %>%
select(pidp,wave,age_dv,sex_dv,ethn_dv,sf1_dv,bmi_dv,sf12pcs_dv,fihhmnnet1_dv,sf12mcs_dv) %>%
filter(wave == "1", age_dv<=50)%>%
mutate(pipd = row_number(),age=age_dv, sex=sex_dv, ethnicity = ethn_dv, general_health=sf1_dv,
bmi=bmi_dv, physical_component_score=sf12pcs_dv, mental_component_score=sf12mcs_dv, household_income=fihhmnnet1_dv)%>%
select(-pipd,-age_dv,-sex_dv,-ethn_dv,-sf1_dv,-bmi_dv,-sf12pcs_dv,-sf12mcs_dv,-fihhmnnet1_dv)
I hope this is correct, here is the dput:
Essentially im just trying to explore BMI but i dont know if I can just plot these or have to assign the numbers to a label like it already is done in haven labels
dput(head(Data))
structure(list(pidp = structure(c(68001367, 68006127, 68008167,
68009527, 68010207, 68010887), label = "cross-wave person identifier (public release)", format.stata = "%12.0g"),
wave = structure(c(1, 1, 1, 1, 1, 1), label = "interview wave", format.stata = "%8.0g"),
age = structure(c(39, 39, 38, 31, 24, 45), label = "Age, derived from dob_dv and intdat_dv", format.stata = "%8.0g"),
sex = structure(c(1, 2, 2, 1, 2, 2), label = "Sex, derived", format.stata = "%8.0g", labels = c(Male = 1,
Female = 2), class = c("haven_labelled", "vctrs_vctr", "double"
)), ethnicity = structure(c(1, 1, 1, 1, 1, 1), label = "Ethnic group (derived from multiple sources)", format.stata = "%8.0g", labels = c(`white uk` = 1,
irish = 2, `gypsy or irish traveller` = 3, `any other white background` = 4,
`white and black caribbean` = 5, `white and black african` = 6,
`white and asian` = 7, `any other mixed background` = 8,
indian = 9, pakistani = 10, bangladeshi = 11, chinese = 12,
`any other asian background` = 13, caribbean = 14, african = 15,
`any other black background` = 16, arab = 17, `any other ethnic group` = 97
), class = c("haven_labelled", "vctrs_vctr", "double")),
general_health = structure(c(2, 4, 5, 3, 1, 1), label = "General health", format.stata = "%8.0g", labels = c(excellent = 1,
`very good` = 2, good = 3, fair = 4, `or Poor?` = 5), class = c("haven_labelled",
"vctrs_vctr", "double")), bmi = structure(c(29.6, 38.8, 21.5,
24.2, 25, 25.5), label = "Body Mass Index", format.stata = "%12.0g")
Thanks for posting an example of your data with dput(). The format of the data you have posted suggests that it has somehow become a list rather than a data frame. You need to convert it to a data frame - as you're using haven I would stick with the tidyverse and do it with as_tibble().
Similarly, you want the labels rather than the underlying integers. You can simply apply as_factor to the whole data frame to do this.
Your data is then ready to be piped to ggplot2. For example:
library(dplyr)
library(ggplot2)
library(haven)
Data |>
as_tibble() |>
as_factor() |>
ggplot() +
geom_boxplot(aes(x=sex, y=bmi))

why do I get Error in `vec_as_location()`: when computing count and full join function?

I made the objects with the variable name I want and selected variables for imported data. But when i use full_join or count, it kept giving me the Error in vec_as_location():. Does anyone know how to avoid this error? The code I wrote a month ago also got this error. But a month ago it worked.
vars <- c("pidp", "cb_age")
wave1 <- read_dta("./data/dresp_w.dta",
col_select = vars)
vars2 <- c("pidp", "cb_sex")
wave2 <- read_dta("./data/dresp_w.dta",
col_select = vars2)
wave12 <- full_join(wave1, wave2, by = "pidp")
count(wave1,cb_sex)
The output for dput(head(wave1))
dput(head(wave2)) would be:
structure(list(pidp = structure(c(76165, 280165, 599765, 732365,
1587125, 3424485), label = "Cross-wave Person Identifier (Public Release)", format.stata = "%12.0g"),
cb_age = structure(c(37, 40, 33, 34, 54, 84), label = "Age - derived", format.stata = "%8.0g", labels = c(Missing = -9,
Inapplicable = -8, Refusal = -2, `Don't know` = -1), class = c("haven_labelled",
"vctrs_vctr", "double"))), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
structure(list(pidp = structure(c(76165, 280165, 599765, 732365,
1587125, 3424485), label = "Cross-wave Person Identifier (Public Release)", format.stata = "%12.0g"),
cb_sex = structure(c(2, 2, 2, 1, 2, 2), label = "Respondent sex", format.stata = "%8.0g", labels = c(Missing = -9,
Inapplicable = -8, Refusal = -2, `Don't know` = -1, Male = 1,
Female = 2), class = c("haven_labelled", "vctrs_vctr", "double"
))), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))
Thank you for your help!!
It seems to work fine with dplyr packageVersion -1.0.9
dplyr::count(wave2, cb_sex)
# A tibble: 2 × 2
cb_sex n
<dbl+lbl> <int>
1 1 [Male] 1
2 2 [Female] 5

R Error (subscript) logical subscript too long

I am attempting to adjust my standard errors by running the following code:
#################################################################################
# Metaregression -- Academic Model
#################################################################################
# save list of moderators to include
terms_1 <- c("Targeted_c",
"MOOSES_Rating_5_c", "Middle_c","High_c")
# Student_report_c is reference variable
# format moderators into formula (an R-specifc type)
formula_academic <- reformulate(termlabels = c(terms_1))
formula_academic
# estimate a covariance matrix
V_list_academic <- impute_covariance_matrix(vi = full_academic$variance, #known correlation vector
cluster = full_academic$Study_ID, #study ID
r = 0.80) #assumed correlation
MVfull_academic <- rma.mv(yi=ES_adjusted, #effect size
V = V_list_academic, #variance (ThIS IS WHAt CHANGES FROM HEmodel)
mods = formula_academic, #ADD COVS HERE
random = ~1 | Study_ID/ES_ID, #nesting structure
test= "t", #use t-tests
data=full_academic, #define data
method="REML") #estimate variances using REML
MVfull_academic
#t-tests of each covariate #
MVfull.coef_academic <- coef_test(MVfull_academic,#estimation model above
cluster=full_academic$Study_ID, #define cluster IDs
vcov = "CR2") #estimation method (CR2 is best)
MVfull.coef_academic
This is the part that returns an error:
MVfull_academic
#t-tests of each covariate #
MVfull.coef_academic <- coef_test(MVfull_academic,#estimation model above
cluster=full_academic$Study_ID, #define cluster IDs
vcov = "CR2") #estimation method (CR2 is best)
MVfull.coef_academic
The error is the following:
Error in x[fac == f, fac == f, drop = FALSE] :
(subscript) logical subscript too long
It sounds like something is not fitting within my data, but I'm not sure what it could be. It looks like everything in the daataset is the same lenghth. How to I fix this error?
Here is my data:
structure(list(APA = structure(c("Barr et al. (2015)", "Blair & Ravor (2014)",
"Bos et al. (2019)", "Bos et al. (2019)", "Conduct Problems Prevention Research Group (1999)",
"Conduct Problems Prevention Research Group (1999)"), label = "APA", format.stata = "%215s"),
Intervention = structure(c("Facing History and Ourselves",
"Tools of the Mind", "BARR", "BARR", "Fast Track (Selective)",
"Fast Track (Selective)"), label = "Intervention", format.stata = "%74s"),
TxCluster = structure(c(32, 16, 1, 1, 27, 27), label = "Tx.\nCluster", format.stata = "%10.0g"),
ControlCluster = structure(c(30, 13, 1, 1, 27, 27), label = "Control.\nCluster", format.stata = "%10.0g"),
UnitofCluster = structure(c("schools", "schools", "", "",
"schools", "schools"), label = "Unit of Cluster", format.stata = "%10s"),
TxN = structure(c(587, 408, 1467, 1466, 419, 275), label = "Tx.N", format.stata = "%10.0g"),
ControlN = structure(c(700, 282, 1916, 1910, 418, 276), label = "Control.N", format.stata = "%10.0g"),
Total_N = structure(c(1287, 690, 3383, 3376, 837, 551), label = "Total_N", format.stata = "%10.0g"),
WebsiteCategoryacademicemot = structure(c("Academic", "Academic",
"Academic", "Academic", "Academic", "Academic"), label = "Website Category (academic, emotion, relations, problem behavior)", format.stata = "%20s"),
MOOSES = structure(c(4, 5, 5, 5, 5, 5), label = "MOOSES rating\n1= cognitive/lower level skills (e.g. emotional recog.; pencil tap", format.stata = "%10.0g"),
ES = structure(c(0.14, 0.13, 0.31, 0.11, -0.01, 0.17), label = "ES", format.stata = "%10.0g"),
TypeofMeasure = structure(c("student self-report", "Standardized assessment",
"school record", "school record", "official report", "standardized assessment"
), label = "Type of Measure", format.stata = "%23s"), ES_ID = structure(c(22,
41, 58, 59, 135, 138), format.stata = "%9.0g"), Study_ID = structure(c(5,
9, 11, 11, 19, 19), label = "group(APA)", format.stata = "%9.0g"),
Targeted = structure(c(0, 0, 0, 0, 0, 0), format.stata = "%9.0g"),
Primary = structure(c(0, 1, 0, 0, 1, 1), format.stata = "%9.0g"),
Middle = structure(c(0, 0, 0, 0, 0, 0), format.stata = "%9.0g"),
High = structure(c(1, 0, 1, 1, 0, 0), format.stata = "%9.0g"),
Significant = structure(c(1, 1, 1, 1, 1, 1), format.stata = "%9.0g"),
MOOSES_Rating_4 = structure(c(1, 0, 0, 0, 0, 0), format.stata = "%9.0g"),
MOOSES_Rating_5 = structure(c(0, 1, 1, 1, 1, 1), format.stata = "%9.0g"),
MOOSES_Rating_4_c = structure(c(0.295774638652802, -0.704225361347198,
-0.704225361347198, -0.704225361347198, -0.704225361347198,
-0.704225361347198), format.stata = "%9.0g"), MOOSES_Rating_5_c = structure(c(-0.253521114587784,
0.746478855609894, 0.746478855609894, 0.746478855609894,
0.746478855609894, 0.746478855609894), format.stata = "%9.0g"),
Targeted_c = structure(c(-0.239436626434326, -0.239436626434326,
-0.239436626434326, -0.239436626434326, -0.239436626434326,
-0.239436626434326), format.stata = "%9.0g"), Primary_c = structure(c(-0.718309879302979,
0.281690150499344, -0.718309879302979, -0.718309879302979,
0.281690150499344, 0.281690150499344), format.stata = "%9.0g"),
Middle_c = structure(c(-0.126760557293892, -0.126760557293892,
-0.126760557293892, -0.126760557293892, -0.126760557293892,
-0.126760557293892), format.stata = "%9.0g"), High_c = structure(c(0.845070421695709,
-0.154929578304291, 0.845070421695709, 0.845070421695709,
-0.154929578304291, -0.154929578304291), format.stata = "%9.0g"),
Full_Sample = structure(c(1287, 690, 3383, 3376, 837, 551
), format.stata = "%9.0g"), Clusters_Total = structure(c(62,
29, 2, 2, 54, 54), format.stata = "%9.0g"), ES_adjusted = structure(c(0.12521980702877,
0.116275534033775, 0.277272433042526, 0.0983869880437851,
-0.00894427206367254, 0.152052626013756), format.stata = "%9.0g"),
SE = structure(c(0.05644915625453, 0.0780460089445114, 0.0353467278182507,
0.0349567793309689, 0.0690869837999344, 0.0861022993922234
), format.stata = "%9.0g"), variance = structure(c(0.0439638122916222,
0.0306105446070433, 0.00127180037088692, 0.001214295392856,
0.02976069226861, 0.100570656359196), format.stata = "%9.0g")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
I just found an NA in my data, I think it may be that!

tapply(dat$`lagged Date`, INDEX = dat$Location, FUN = diff(dat$`lagged Date`))

Can someone explain me why this is not working?
tapply(dat$`lagged Date`, INDEX = dat$Location, FUN = diff(dat$`lagged Date`))
I receive the following error:
Error in match.fun(FUN) : 'diff(dat$lagged Date)' is not a
function, character or symbol
structure(list(`lagged Date` = structure(c(1466306880, 1466307060,
1466307240, 1466307420, 1466307600, 1466307780), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), Location = c(309, 309, 309, 309, 309,
309), Duration = c(0, 0, 0, 0, 0, 0), Latitude = c(53.50205667,
53.501915, 53.50183667, 53.50178833, 53.50184, 53.50186167),
Longitude = c(-3.354733333, -3.354096667, -3.353838333, -3.353673333,
-3.353711667, -3.353741667), `Number of Records` = c(1, 1,
1, 1, 1, 1), Speed = c(0.9, 0, 0, 0, 0, 0), `Sum of Var` = c(38,
38, 38, 38, 38, 38), check = c(0, 0, 0, 0, 0, 0)), .Names = c("lagged Date",
"Location", "Duration", "Latitude", "Longitude", "Number of Records",
"Speed", "Sum of Var", "check"), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
thank you!
I'm not sure what you want to achieve, but using only diff as the FUN part works and produces this output:
tapply(dat$`lagged Date`, INDEX = dat$Location, FUN = diff)
$`309`
Time differences in mins
[1] 3 3 3 3 3
If you want to convert the output into hours, you can do that by selecting only the values of the difftime-list object and convert those:
as.numeric(tapply(dat$`lagged Date`, INDEX = dat$Location, FUN = diff)[[1]], units = "hours")
Output then looks like this:
[1] 0.05 0.05 0.05 0.05 0.05

Resources