R: Splitting comma-separated values from one column into a new dataframe (or how to use strsplit results)
<p>I have a list of test results from two different groups that I've brought into R, and I have very little experience with R.  My current dataframe looks like this:</p>

<pre><code>Samples &lt;- c("A","B","C","D","E")
Test1 &lt;- c("1","","300","NA","100, 2")
Test2 &lt;- c("2","5000","3, 4000, 5","3","")
df &lt;- data.frame(Samples, Test1, Test2)
</code></pre>

<p>And I'd like to (1) Break the cells with multiple, comma-separated values into different columns, (2) Fix the units of the tests so they match up - Test1 in this is sometimes reported on a scale which is off by a factor of 100 while Test2 reports are off by a factor of 1000, and (3) average the resulting values back into one column.</p>

<p>I started off like this:</p>

<pre><code>#Replace blank cells with NA
df[df==""] &lt;- "NA"
#Split cells with commas in them
split2 &lt;- strsplit(df$Test2, split = "\\,")
</code></pre>

<p>And promptly got stuck.  How do I get the list created back into some sort of dataframe?  I don't know how many comma separated strings are in my giant dataset's column.  Most rows only have one result, but I know there are several with two and at least one with somewhere around eight. My two attempts to build a new dataframe (below) did not go well. </p>

<pre><code>do.call(rbind, lapply(split2))
Error in match.fun(FUN) : argument "FUN" is missing, with no default
data.frame(t(sapply(split2,c)))
  X1   X2           X3 X4 X5
1  2 5000 3,  4000,  5  3   
</code></pre>

<p>I'm sure there's some way to do what I'm looking to do while still using the list and almost certainly some way to do this with a function/for loop.  Any suggestions would be greatly appreciated.</p>

<p>Eventually, I'd like the data to look like this:</p>

<pre><code>    Samples Test1 Test2
1       A     1     2
2       B    NA     5
3       C     3     4
4       D    NA     3
5       E   1.5    NA
</code></pre>