Question



How to keep the same structure of a data frame when appying functions to it?

Dear Dr. Ballings,

In regards to exercise 10 where we have to find a faster way to find the ranges for each column, I am having trouble finding a range and
still keeping the same structure as the original data frame "ranges."

I have found a way to get the ranges of each column using the apply function, but then I need to transpose that data frame and use
as.data.frame. When I run str(), it gives me a structure that differs from the structure of "ranges." Is there a way to transpose a data
frame without losing information?

Even if I just use range within the function sapply on the data frame "df," it really changes the structure. I've found a way to get the
same numbers using sapply, but not the same structure--- help! How do I keep the same structure?

John Bills





Answers and follow-up questions





Answer or follow-up question 1

I should add that adding drop=FALSE does not change anything (unless I'm adding that in the incorrect place???)


Answer or follow-up question 2

Dear John,

You're on track to finding the solution. You're mentioning all the correct elements. Try a couple of variants and you'll get there.

When I say 'have the same structure' I mean that the result of str() on the ranges object from the loop needs to be the the same as the
result of str() on the ranges object that you create.

Michel Ballings


Answer or follow-up question 3

Dr. Ballings,

This is the str() that I get for both:

http://s10.postimg.org/dj6fwwtbd/Screen_Shot_2016_01_29_at_3_50_36_PM.png

Is this considered correct? The "X1" and the V1" are what concern me.

John




Answer or follow-up question 4

Found my error! Disregard my last message!

Thanks!



Sign in to be able to add an answer or mark this question as resolved.