Mipam Bruining
2010-Nov-01 11:55 UTC
[R] number of items to replace is not a multiple of replacement length
Hey all, I am writing a function in which I will have a matrix of 4 columns and a variable amount of rows. The first to columns will always contain be of the Character type, the third and fourth columns can be a variation of data types, usually characters and integers, but sometimes lists or matrices. At one point the code makes, for each row, a copy of that row, then it makes some adjustments to the values in the third column, and then it overwrites the original matrix row with the copy. It is here that I get the error from my subject title, "number of items to replace is not a multiple of replacement length". This is strange because the items to replace in this instance seem to me and should be an exact copy of the replacement, save that the number 0 in this particular instance is to be replaced with the number 58. If I print the variables in question I get this: (added some tabs to allign the columns for readability)>print(parameterList[aCounter, 3])[[1]] [,1] [,2] [,3] [,4] exampleSingleSelect "Insert single selection box title here" "SingleSelect" 1 List,3 exampleSlider "Insert slider bar title here" "Slider" 0 List,2>print(blaah)[,1] [,2] [,3] [,4] exampleSingleSelect "Insert single selection box title here" "SingleSelect" 1 List,3 exampleSlider "Insert slider bar title here" "Slider" 58 List,2 The command that caused the error is this:>parameterList[aCounter, 3] <<- blaahI hope I've given enough information for someone else to be able to make sense of it all. If not I'd be happy to supply additional code or info on how the script works, but I think that most of it would be irrelevant and complicated. Many thanks in advance, Mipam Bruining [[alternative HTML version deleted]]
Ivan Calandra
2010-Nov-01 12:42 UTC
[R] number of items to replace is not a multiple of replacement length
Hi, I think you _*should*_ provide more: - the objects you have, such as parameterList and blaah: copy the output from dput(object) in your next e-mail - the _*relevant*_ code for how you've built them I guess you will get more and better answers if you give a clear and complete description of your problem. Ivan Le 11/1/2010 12:55, Mipam Bruining a ?crit :> Hey all, > > I am writing a function in which I will have a matrix of 4 columns and a > variable amount of rows. > The first to columns will always contain be of the Character type, the third > and fourth columns > can be a variation of data types, usually characters and integers, but > sometimes lists or matrices. > > At one point the code makes, for each row, a copy of that row, then it makes > some adjustments > to the values in the third column, and then it overwrites the original > matrix row with the copy. It is here > that I get the error from my subject title, "number of items to replace is > not a multiple of replacement length". > This is strange because the items to replace in this instance seem to me and > should be an exact copy of the > replacement, save that the number 0 in this particular instance is to be > replaced with the number 58. > > If I print the variables in question I get this: (added some tabs to allign > the columns for readability) > >> print(parameterList[aCounter, 3]) > [[1]] > [,1] > [,2] [,3] [,4] > exampleSingleSelect "Insert single selection box title here" > "SingleSelect" 1 List,3 > exampleSlider "Insert slider bar title here" > "Slider" 0 List,2 > >> print(blaah) > [,1] > [,2] [,3] [,4] > exampleSingleSelect "Insert single selection box title here" "SingleSelect" > 1 List,3 > exampleSlider "Insert slider bar title here" > "Slider" 58 List,2 > > > The command that caused the error is this: > >> parameterList[aCounter, 3]<<- blaah > > I hope I've given enough information for someone else to be able to make > sense of it all. If not I'd be happy to supply additional code > or info on how the script works, but I think that most of it would be > irrelevant and complicated. > > Many thanks in advance, > Mipam Bruining > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
Mipam Bruining
2010-Nov-01 14:41 UTC
[R] number of items to replace is not a multiple of replacement length
Hello, I just wanted to say that I just stumbled upon the solution by accident! After your comment on lists I searched for some more information on vectors, lists and matrices. I read that using double ['s instead of singles drops the names of the dimensions used. I then gave parameterList[aCounter, 3] <<- blaah instead of parameterList[[aCounter, 3]] <<- blaah a quick try and voila! In hindsight I guess I was just thinking to much in python style where matrices dont have dimension naming. (python being the language in which I first learned to program and quite similar to R) But thanks for your help anyway! Mipam Bruining 2010/11/1 Ivan Calandra <ivan.calandra@uni-hamburg.de>> Hi, > > Not sure what the problem is, but you have weird data structures. I mean, > having lists within matrices is possible (I've learned it last week!), but > it is really confusing. > > You didn't provide every object like the complete parameterList and > aCounter, but the only difference I see in > > aParameterValue <- as.matrix(parameterList[[aCounter,3]]) > ... > blaah <<- aParameterValue > ... > parameterList[aCounter, 3] <<- blaah > between the original parameterList[[aCounter,3]] and the new one is that > your aParameterValue, and therefore blaah, is a matrix, and a weird one, > that you try to assign into a non-matrix object. > > Have you tried without as.matrix()? What about storing everything in a list > and using indexing? > > HTH, > Ivan > > > Le 11/1/2010 14:43, Mipam Bruining a écrit : > >> Hello, >> >> here are the results from using dput() on the objects in question. >> >> >dput(blaah) >> structure(list("Insert single selection box title here", "Insert slider >> bar title here", >> "SingleSelect", "Slider", 1, 50, list("Option a", "Option b", >> "Option c"), list(0, 100)), .Dim = c(2L, 4L), .Dimnames = list( >> c("exampleSingleSelect", "exampleSlider"), NULL)) >> >> >dput(parameterList[aCounter, 3]) >> list(structure(list("Insert single selection box title here", >> "Insert slider bar title here", "SingleSelect", "Slider", >> 1, 0, list("Option a", "Option b", "Option c"), list(0, 100)), .Dim >> c(2L, >> 4L), .Dimnames = list(c("exampleSingleSelect", "exampleSlider" >> ), NULL))) >> >> >> The matrix parameterList was constructed as followed: >> >> <R script> >> exampleSlider = list("Insert slider bar title here", "Slider", 0, >> list(0,100)) >> exampleSingleSelect = list("Insert single selection box title here", >> "SingleSelect", 1, >> list("Option a", "Option b", "Option c")) >> >> parameterList = rbind(list("more advanced options", "Button", >> rbind(exampleSingleSelect,exampleSlider ), " ") >> ... >> aParameterValue <- as.matrix(parameterList[[aCounter,3]]) >> ... >> blaah <<- aParameterValue >> ... >> parameterList[aCounter, 3] <<- blaah >> >> </R script> >> >> As a side note: The ... represents various other lines. The construction >> parameterList <<- blaah <<- aParameterValue is an ad-hoc solution to using >> global and local variables that I will try to smooth over once it works as >> it is now. To be more specific, the value aParameterValue is a local >> variable that is created in a subfunction which is called upon once for each >> row in parameterList, each time the user presses a button in the created >> tcltk window. >> The blaah variable is a global variable that solves the problem of tlctk >> buttons not being able to receive the returned values from the function it >> calls and parameterList somehow not being able to be updated correctly >> within this function. >> >> I hope anyone can make sense of this story :S >> >> Mipam Bruining >> >> >> 2010/11/1 Ivan Calandra <ivan.calandra@uni-hamburg.de <mailto: >> ivan.calandra@uni-hamburg.de>> >> >> >> Hi, >> >> I think you _*should*_ provide more: >> - the objects you have, such as parameterList and blaah: copy the >> output from dput(object) in your next e-mail >> - the _*relevant*_ code for how you've built them >> >> I guess you will get more and better answers if you give a clear >> and complete description of your problem. >> >> Ivan >> >> Le 11/1/2010 12:55, Mipam Bruining a écrit : >> >> Hey all, >> >> I am writing a function in which I will have a matrix of 4 >> columns and a >> variable amount of rows. >> The first to columns will always contain be of the Character >> type, the third >> and fourth columns >> can be a variation of data types, usually characters and >> integers, but >> sometimes lists or matrices. >> >> At one point the code makes, for each row, a copy of that row, >> then it makes >> some adjustments >> to the values in the third column, and then it overwrites the >> original >> matrix row with the copy. It is here >> that I get the error from my subject title, "number of items >> to replace is >> not a multiple of replacement length". >> This is strange because the items to replace in this instance >> seem to me and >> should be an exact copy of the >> replacement, save that the number 0 in this particular >> instance is to be >> replaced with the number 58. >> >> If I print the variables in question I get this: (added some >> tabs to allign >> the columns for readability) >> >> print(parameterList[aCounter, 3]) >> >> [[1]] >> [,1] >> [,2] [,3] [,4] >> exampleSingleSelect "Insert single selection box title here" >> "SingleSelect" 1 List,3 >> exampleSlider "Insert slider bar title here" >> "Slider" 0 List,2 >> >> print(blaah) >> >> [,1] >> [,2] [,3] [,4] >> exampleSingleSelect "Insert single selection box title here" >> "SingleSelect" >> 1 List,3 >> exampleSlider "Insert slider bar title here" >> "Slider" 58 List,2 >> >> >> The command that caused the error is this: >> >> parameterList[aCounter, 3]<<- blaah >> >> >> I hope I've given enough information for someone else to be >> able to make >> sense of it all. If not I'd be happy to supply additional code >> or info on how the script works, but I think that most of it >> would be >> irrelevant and complicated. >> >> Many thanks in advance, >> Mipam Bruining >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org <mailto: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. >> >> >> -- Ivan CALANDRA >> PhD Student >> University of Hamburg >> Biozentrum Grindel und Zoologisches Museum >> Abt. Säugetiere >> Martin-Luther-King-Platz 3 >> D-20146 Hamburg, GERMANY >> +49(0)40 42838 6231 >> ivan.calandra@uni-hamburg.de <mailto:ivan.calandra@uni-hamburg.de> >> >> >> ********** >> http://www.for771.uni-bonn.de >> http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php >> >> ______________________________________________ >> R-help@r-project.org <mailto: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. >> >> >> > -- > Ivan CALANDRA > PhD Student > University of Hamburg > Biozentrum Grindel und Zoologisches Museum > Abt. Säugetiere > Martin-Luther-King-Platz 3 > D-20146 Hamburg, GERMANY > +49(0)40 42838 6231 > ivan.calandra@uni-hamburg.de > > ********** > http://www.for771.uni-bonn.de > http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php > >[[alternative HTML version deleted]]