[R] Odd behavior of a function within apply
Erin Hodgess
er|nm@hodge@@ @end|ng |rom gm@||@com
Mon Aug 8 19:41:23 CEST 2022
lapply worked perfectly!
Erin Hodgess, PhD
mailto: erinm.hodgess using gmail.com
On Mon, Aug 8, 2022 at 1:29 PM Andrew Simmons <akwsimmo using gmail.com> wrote:
> When you use apply, it converts the input to an array, so all of the
> integer columns in your data frame are converted to character class. I
> would use lapply, sapply, or vapply instead to do this, I think you just
> need to remove the "MARGIN" argument.
>
> On Mon, Aug 8, 2022, 13:25 Erin Hodgess <erinm.hodgess using gmail.com> wrote:
>
>> Hello!
>>
>> I have the following data.frame
>> dput(test1.df[1:10,8:10])
>> structure(list(X1_1_HZP1 = c(48160L, 48198L, 80027L, 48161L,
>> NA, 48911L, NA, 48197L, 48021L, 11355L), X1_1_HBM1_mon = c("December",
>> "June", "August", "", "", "August", "April", "February", "",
>> "December"), X1_1_HBM1_yr = c(2014L, 2018L, 2016L, NA, NA, 1985L,
>> 2019L, 1993L, NA, 1990L)), row.names = c(NA, 10L), class = "data.frame")
>>
>> And the following function:
>> > dput(count1a)
>> function (x)
>> {
>> if (typeof(x) == "integer")
>> y <- sum(is.na(x))
>> if (typeof(x) == "character")
>> y <- sum(x == "")
>> return(y)
>> }
>> When I use the apply function with count1a, I get the following:
>> apply(test1.df[1:10,8:10],2,count1a)
>> X1_1_HZP1 X1_1_HBM1_mon X1_1_HBM1_yr
>> NA 3 NA
>> However, when I do use columns 8 and 10, I get the correct response:
>> apply(test1.df[1:10,c(8,10)],2,count1a)
>> X1_1_HZP1 X1_1_HBM1_yr
>> 2 3
>> >
>> I am really baffled. If I use count1a on a single column, it works fine.
>>
>> Any suggestions much appreciated.
>> Thanks,
>> Sincerely,
>> Erin
>>
>>
>> Erin Hodgess, PhD
>> mailto: erinm.hodgess using gmail.com
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list