Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I getting a function error in seemingly similar R code?
    primarykey
    data
    text
    <p>First, this is a very basic question that I'm unsure of how to phrase. If the question is a duplicate (though I checked using what I thought might be appropriate phrasing), I'll obviously retract and appreciate the link. </p> <p>Second, I am <em>sure</em> there is an easier way to do what I'm trying, but don't want to get off-track. </p> <p>OK. I'm attempting to just get a table of column proportions from a matrix of 0/1's (the proportion of 1's conditional on a value of another variable, which is PARTY in this case). </p> <p>my data.frame is m103, and of dimensions (437,91) and the following process works (as in, produces what I want): </p> <pre><code>prop.table(as.matrix(ddply(m103, .(PARTY), sum, na.rm=T))) </code></pre> <p>But of course, I want to actually keep the output, and this is where the error arises. If I do this: </p> <pre><code>a &lt;- prop.table(as.matrix(ddply(m103, .(PARTY), sum, na.rm=T))) </code></pre> <p>Things are great. But IMMEDIATELY after this, if I try: </p> <pre><code>m103.avg.prop &lt;- prop.table(as.matrix(ddply(m103, .(PARTY), sum, na.rm=T))) </code></pre> <p>I get the error: </p> <pre><code>Error in FUN(X[[2L]], ...) : only defined on a data frame with all numeric variables </code></pre> <p>I'd like to keep a rational naming scheme going in my code (which the second example would continue), but I can't tell if this has something to do with what I've tried to assign the output to, or something else. </p> <p>Many thanks! </p> <p>EDIT: Let's see if I can be more explicit</p> <pre><code>#Data import m103 &lt;- read.csv("103_members_party.csv", header=T) #See the first few rows/columns m103[1:5,1:5] #Produces this: ID PARTY X930 X461 X137 1 15245 100 0 0 0 2 15000 100 0 0 0 3 29108 200 0 0 0 4 15001 100 0 0 0 5 29132 100 0 0 0 #Sum and get col percentages by PARTY (sums the 1's when PARTY==100, PARTY==200, etc) #WITHOUT assigning to anything prop.table(as.matrix(ddply(m103, .(PARTY), sum, na.rm=T))) #Produces: PARTY V1 [1,] 1.122515e-05 0.580000465 [2,] 2.245030e-05 0.416619418 [3,] 3.681849e-05 0.003309623 #With assignment to a a &lt;- prop.table(as.matrix(ddply(m103, .(PARTY), sum, na.rm=T))) a #Produces PARTY V1 [1,] 1.122515e-05 0.580000465 [2,] 2.245030e-05 0.416619418 [3,] 3.681849e-05 0.003309623 #Now, assignment to m103.avg.prop m103.avg.prop &lt;- prop.table(as.matrix(ddply(m103, .(PARTY), sum, na.rm=T))) #results in error: Error in FUN(X[[2L]], ...) : only defined on a data frame with all numeric variables </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload