[R] Populating then sorting a matrix and/or data.frame

Michael Bedward michael.bedward at gmail.com
Thu Nov 11 11:00:17 CET 2010


You can use rbind as in your original post, but if you've got a mix of
character and numeric data start with a data.frame rather than a
matrix.

Michael

On 11 November 2010 20:30, Noah Silverman <noah at smartmediacorp.com> wrote:
> That makes perfect sense.
>
> Since I need to build up the results table sequentially as I iterate through
> the data, how would you recommend it??
>
> Thanks,
>
> -N
>
> On 11/11/10 12:03 AM, Michael Bedward wrote:
>>
>> All values in a matrix are the same type, so if you've set up a matrix
>> with a character column then your numeric values will also be stored
>> as character. That would explain why they are being converted to
>> factors.  It would also explain why your query isn't working.
>>
>> Michael
>>
>>
>> On 11 November 2010 18:40, Noah Silverman<noah at smartmediacorp.com>  wrote:
>>>
>>> That was a typo.
>>>
>>> It should have read:
>>> results[results$one<  100,]
>>>
>>> It does still fail.
>>>
>>> There is ONE column that is text.  So my guess is that R is seeing that
>>> and
>>> assuming that the entire data.frame should be factors.
>>>
>>> -N
>>>
>>> On 11/10/10 11:16 PM, Michael Bedward wrote:
>>>>
>>>> Hello Noah,
>>>>
>>>> If you set these names...
>>>>>
>>>>> names(results)<- c("one", "two", "three")
>>>>
>>>> this won't work...
>>>>>
>>>>> results[results$c<    100,]
>>>>
>>>> because you don't have a column called "c" (unless that's just a typo
>>>> in your post).
>>>>
>>>>> I tried making it a data.frame with
>>>>> foo<- data.frame(results)
>>>>>
>>>>> But that converted all the numeric values to factors!!!
>>>>
>>>> Not sure what's going on there. If 'results' is a numeric matrix you
>>>> should get a data.frame with numeric cols since under the hood this is
>>>> just calling the as.data.frame function.
>>>>
>>>> Michael
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> On 11 November 2010 16:02, Noah Silverman<noah at smartmediacorp.com>
>>>>  wrote:
>>>>>
>>>>> Hi,
>>>>>
>>>>> I have a process in R that produces a lot of output.  My plan was to
>>>>> build
>>>>> up a matrix or data.frame "row by row", so that I'll have a nice object
>>>>> with
>>>>> all the resulting data.
>>>>>
>>>>> I started with:
>>>>> results<- matrix(ncol=3)
>>>>> names(results)<- c("one", "two", "three")
>>>>>
>>>>> Then, when looping through the data:
>>>>> results<- rbind(results, c(a,b,c))
>>>>>
>>>>> This seems to work fine. BUT, my problem arises when I want to filter,
>>>>> sort,
>>>>> etc.
>>>>>
>>>>> I tried (thinking like a data.frame):
>>>>> results[results$c<    100,]
>>>>>
>>>>> But that fails.
>>>>>
>>>>> I tried making it a data.frame with
>>>>> foo<- data.frame(results)
>>>>>
>>>>> But that converted all the numeric values to factors!!!  Which causes a
>>>>> whole mess of problems.
>>>>>
>>>>> Any ideas??
>>>>>
>>>>> -N
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>>>>
>>>
>
>



More information about the R-help mailing list