Don, that was great - thanks so much!
...and you are right rbind() being expensive, but my dataframe will always
stay that small (max. twice as large).
Chaehan
On Thu, Feb 11, 2010 at 4:50 PM, Don MacQueen <macq@llnl.gov> wrote:
> You have a mistake in how you're setting up the object named
"result"
> before the loop.
>
> You set result <- latentVariableNames. It is a vector of length 6, when
you
> call the function with LV. The printout from the first iteration shows
this.
>
> But then you rbind result with a data frame that has three columns. These
> are basically incompatible, but R (apparently silently) wraps the vector of
> length 6 into two rows of three columns.
>
> Here's an example:
>
> foo <- letters[1:6]
>> names(foo) <- c('a','b','c')
>> foo
>>
> a b c <NA> <NA> <NA>
> "a" "b" "c" "d" "e"
"f"
>
> rbind(foo, data.frame(a='X', b='Y',
c='Z',stringsAsFactors=FALSE))
>>
> a b c
> 1 a b c
> 2 X Y Z
>
> What I would do is something like this (untested):
>
> loopCronbach <- function(latentVariableNames, groupingVariable) {
> n <- length(latentVariableNames)
> tmp1 <- tmp2 <- numeric(n)
>
> for (i in 1:n) {
> tmp1[i] <- calculateIndividualCronbach(get(latentVariableNames[i]))
> tmp2[i] <- calculateGroupCronbach(get(latentVariableNames[i]),
> groupingVariable)
> }
>
> result <-
> data.frame(latentVariable=latentVariableNames,Indiv=tmp1,Group=tmp2)
>
> names(result) <- c("latentVariable", "Indiv",
"Group")
> result ## no need to use return()
> }
>
> Building up a dataframe row by row using rbind() is in general an expensive
> way to do things -- although in this instance it's such a small
dataframe
> that it doesn't matter.
>
> -Don
>
>
> At 10:56 AM +0100 2/11/10, Chaehan So wrote:
>
>> Ok, you're right - may I rephrase:
>> How should I modify the assignment of result <- latentVariableNames
>> so it produces the output without the first line?
>> I thought result <- NULL should do the job, but it didn't
because of the
>> following names(result) assignment (which I understand, but not how I
>> can workaround it).
>>
>> Here's the output and code again:
>>
>> latentVariable Indiv Group
>> 1 rPlanning rIterat rTDD
>> 2 rPlanning 0.79 0.84
>> 3 rIterat 0.79 0.83
>> 4 rTDD 0.9 0.96
>> 5 rStandup 0.83 0.82
>> 6 rRetros 0.9 0.95
>> 7 rAccess 0.91 0.92
>> 8 rAccTest 0.87 0.9
>>
>> #####
>> LV <- c("rPlanning", "rIterat",
"rTDD", "rStandup", "rRetros",
"rAccess",
>> "rAccTest")
>> #####
>> loopCronbach <- function(latentVariableNames, groupingVariable)
>> {
>> result <- latentVariableNames
>> names(result) <- c("latentVariable", "Indiv",
"Group")
>> for (currentName in latentVariableNames)
>> {
>> print(currentName)
>> print(result)
>> tmp1 <- calculateIndividualCronbach(get(currentName))
>> tmp2 <- calculateGroupCronbach(get(currentName), groupingVariable)
>> result <- rbind(result,data.frame(latentVariable=currentName,
>> Indiv=tmp1,Group=tmp2))
>> }
>> return(result)
>> }
>>
>> On Thu, Feb 11, 2010 at 3:31 AM, jim holtman <jholtman@gmail.com>
wrote:
>>
>> It doing exactly what you asked it to do. You have the assignment:
>>>
>>> result <- latentVariableNames
>>>
>>> and then you print it out in the loop. What were you expecting?
>>>
>>> On Wed, Feb 10, 2010 at 9:06 PM, Chaehan So
<chaehan.so@gmail.com>
>>> wrote:
>>>
>> > > Dear r-helpers,
>> > >
>> > > why do I get an output in the first iteration of the
for-loop
>> > > which contains the string values of the input vector,
>> > > and how can I avoid that?
>>
>>> >
>>> > Here's the output (only line 1 is wrong)
>>> >
>>> > latentVariable Indiv Group
>>> > 1 rPlanning rIterat rTDD
>>> > 2 rPlanning 0.79 0.84
>>> > 3 rIterat 0.79 0.83
>>> > 4 rTDD 0.9 0.96
>>> > 5 rStandup 0.83 0.82
>>> > 6 rRetros 0.9 0.95
>>> > 7 rAccess 0.91 0.92
>>> > 8 rAccTest 0.87 0.9
>>> >
>>> > #####
>>> > LV <- c("rPlanning", "rIterat",
"rTDD", "rStandup", "rRetros",
>>> "rAccess",
>>> > "rAccTest")
>>> > #####
>>> > loopCronbach <- function(latentVariableNames,
groupingVariable)
>>> > {
>>> > result <- latentVariableNames
>>> > names(result) <- c("latentVariable",
"Indiv", "Group")
>>> > for (currentName in latentVariableNames)
>>> > {
>>> > print(currentName)
>>> > print(result)
>>> > tmp1 <- calculateIndividualCronbach(get(currentName))
>>>
>> > > tmp2 <- calculateGroupCronbach(get(currentName),
groupingVariable)
>>
>>> > result <-
rbind(result,data.frame(latentVariable=currentName,
>>> > Indiv=tmp1,Group=tmp2))
>>> > }
>>> > return(result)
>>> > }
>>> >
>>> >
>>> > a <- loopCronbach(LV, u_proj)
>>> >
>>> > [[alternative HTML version deleted]]
>>> >
>>> > ______________________________________________
>>> > R-help@r-project.org mailing list
>>> > 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.
>>> >
>>>
>>>
>>>
>>> --
>>> Jim Holtman
>>> Cincinnati, OH
>>> +1 513 646 9390
>>>
>>> What is the problem that you are trying to solve?
>>>
>>>
>>
>>
>> --
>> Humboldt University Berlin, Germany
>> Institute of Psychology
>> Rudower Chaussee 18, Room 1221
>> 12489 Berlin
>> Germany
>> Office: +49 30 2093 - 9337
>> Mobile: +49 171- 626 9373
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@r-project.org mailing list
>> 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.
>>
>
>
> --
> --------------------------------------
> Don MacQueen
> Environmental Protection Department
> Lawrence Livermore National Laboratory
> Livermore, CA, USA
> 925-423-1062
> --------------------------------------
>
--
Humboldt University Berlin, Germany
Institute of Psychology
Rudower Chaussee 18, Room 1221
12489 Berlin
Germany
Office: +49 30 2093 - 9337
Mobile: +49 171- 626 9373
Sent from Berlin, BE, Germany
[[alternative HTML version deleted]]