How can I calculate the slope of replication at a given area

I’m trying to calculate the slope in R using the lm() function. I have a data set that contains the points X1-X20 with calculated area and mean by log function (see below). How can I calculate and create a new column for slope in data frame?

My approach has been to do this:

coefLM <- function(x) {
coef(lm(log(mean)~log(area), data = x))[2]

And getting the following error message:

coefs <- sapply(tar.all[-1,], coefLM)
Error in eval(predvars, data, env) :
numeric 'envir' arg not of length one

Here is my data frame, name tar.all:

tar.all <-
structure(list(X1 = c(0L, 1481L, 2073L, 2485L, 2224L, 2260L, 
2526L, 2832L, 1817L, 1540L, 1948L, 917L, 2210L, 1894L, 2759L, 
2629L, 2726L, 2368L, 2192L, 1946L, 2720L, 1944L, 2248L, 2105L, 
2159L, 2153L, 2011L, 2095L, 1907L, 2527L, 1621L, 2015L, 2383L, 
2241L, 2009L, 2420L, 2247L, 2029L, 2109L, 1979L, 2165L, 2181L, 
2064L), X2 = c(0L, 2087L, 2280L, 2083L, 1191L, 2466L, 2229L, 
1755L, 1234L, 2312L, 1350L, 2251L, 2436L, 2062L, 1894L, 2387L, 
2048L, 2198L, 2011L, 2331L, 2175L, 2152L, 2300L, 1941L, 2133L, 
2174L, 2098L, 2126L, 2284L, 2015L, 2421L, 1692L, 2455L, 1962L, 
2101L, 2199L, 2318L, 2017L, 2097L, 1992L, 2083L, 2198L, 2064L
), X3 = c(0L, 2231L, 1913L, 1736L, 1881L, 1480L, 2463L, 2207L, 
1905L, 1062L, 2625L, 2123L, 2198L, 1799L, 2486L, 1727L, 2633L, 
2579L, 2601L, 2498L, 2160L, 1952L, 2074L, 2688L, 2543L, 2324L, 
1658L, 2392L, 1720L, 1891L, 2266L, 1943L, 2344L, 2431L, 2037L, 
2428L, 2066L, 2174L, 2097L, 2226L, 2206L, 2160L, 2167L), X4 = c(0L, 
894L, 421L, 1060L, 2743L, 2096L, 2227L, 2696L, 2393L, 1755L, 
2176L, 1695L, 1806L, 1757L, 1778L, 2408L, 1830L, 1987L, 1786L, 
2547L, 2286L, 1916L, 2129L, 2108L, 2113L, 2277L, 2058L, 1920L, 
1673L, 2151L, 2445L, 2097L, 1895L, 2443L, 2453L, 1954L, 2318L, 
2145L, 1933L, 1992L, 2233L, 2160L, 2096L), X5 = c(0L, 2633L, 
1913L, 2566L, 2557L, 1389L, 2341L, 2102L, 2794L, 1864L, 2138L, 
1746L, 2357L, 2479L, 2405L, 1385L, 1776L, 2198L, 1976L, 2339L, 
2418L, 2010L, 2115L, 2271L, 1958L, 2240L, 1853L, 2100L, 1695L, 
2133L, 2078L, 1867L, 2243L, 1941L, 2418L, 2443L, 2318L, 1901L, 
2318L, 2258L, 2218L, 2169L, 2168L), X6 = c(0L, 1437L, 1449L, 
1643L, 1229L, 2020L, 2635L, 2593L, 2292L, 1260L, 2771L, 2779L, 
1873L, 2428L, 1459L, 1768L, 1112L, 1974L, 1946L, 2500L, 2170L, 
2265L, 1998L, 2316L, 2459L, 2148L, 2503L, 2384L, 2141L, 2262L, 
2078L, 2243L, 1866L, 2429L, 2418L, 2358L, 2340L, 1922L, 2308L, 
2228L, 2092L, 2157L, 2187L), X7 = c(0L, 2079L, 1794L, 970L, 249L, 
1570L, 443L, 2497L, 2239L, 2858L, 2405L, 1773L, 2469L, 2078L, 
1593L, 2358L, 2421L, 2551L, 2497L, 1955L, 2300L, 1701L, 2111L, 
2178L, 2543L, 2343L, 2209L, 2353L, 2394L, 1716L, 1739L, 2466L, 
2218L, 1909L, 2345L, 2392L, 2305L, 2196L, 2328L, 2306L, 2092L, 
2070L, 2195L), X8 = c(0L, 2513L, 1381L, 2482L, 2744L, 2327L, 
2243L, 1889L, 2583L, 1457L, 1185L, 1988L, 2113L, 2184L, 1747L, 
2136L, 2197L, 2212L, 1752L, 2118L, 2439L, 2438L, 1939L, 2171L, 
1836L, 1673L, 1774L, 2100L, 2194L, 2287L, 2054L, 1990L, 2031L, 
2293L, 2293L, 2259L, 2095L, 2017L, 2097L, 2226L, 2197L, 2160L, 
2167L), X9 = c(0L, 2836L, 2494L, 2730L, 1916L, 2700L, 2357L, 
1406L, 943L, 1574L, 2220L, 2243L, 2827L, 1213L, 2146L, 1524L, 
1839L, 2221L, 2215L, 2136L, 1944L, 1815L, 2669L, 2242L, 1828L, 
1855L, 1771L, 2574L, 2377L, 2304L, 2355L, 2165L, 2238L, 2145L, 
2026L, 2264L, 2318L, 2260L, 2255L, 2306L, 2206L, 2192L, 2163L
), X10 = c(0L, 494L, 2255L, 1851L, 629L, 2362L, 1853L, 968L, 
1377L, 2265L, 1702L, 2217L, 2349L, 1991L, 1999L, 2502L, 1437L, 
1752L, 2081L, 2547L, 2440L, 1736L, 2423L, 1939L, 2189L, 2437L, 
2116L, 2249L, 2148L, 1981L, 1745L, 2101L, 1877L, 2282L, 2037L, 
2408L, 2426L, 2121L, 2326L, 2316L, 1899L, 2070L, 2209L), X11 = c(0L, 
1988L, 1002L, 1203L, 1765L, 2502L, 1821L, 2467L, 2017L, 2821L, 
1171L, 1685L, 1325L, 1721L, 1385L, 1686L, 1707L, 2573L, 1934L, 
2692L, 2303L, 2224L, 2154L, 1941L, 2313L, 2535L, 1925L, 2233L, 
1738L, 2060L, 2255L, 1796L, 2078L, 1919L, 2264L, 1997L, 2212L, 
2005L, 2243L, 2145L, 2000L, 2199L, 1891L), X12 = c(0L, 397L, 
1930L, 2054L, 2197L, 676L, 1072L, 2146L, 1886L, 2307L, 1469L, 
1860L, 1582L, 2708L, 2534L, 2234L, 1796L, 2237L, 2352L, 2355L, 
2146L, 1769L, 1837L, 2318L, 2405L, 2483L, 2054L, 2382L, 2584L, 
2154L, 2050L, 1709L, 2077L, 2044L, 2330L, 2197L, 2420L, 2053L, 
2235L, 2357L, 2092L, 2027L, 2057L), X13 = c(0L, 1611L, 2270L, 
2859L, 2728L, 851L, 2344L, 1871L, 2182L, 2476L, 1240L, 2685L, 
1802L, 2146L, 1653L, 1934L, 2129L, 2692L, 2031L, 2436L, 1813L, 
2559L, 2263L, 2006L, 1991L, 2141L, 2225L, 2096L, 2469L, 2225L, 
2278L, 1997L, 2165L, 2289L, 2158L, 2193L, 2167L, 2133L, 2258L, 
2081L, 2202L, 2173L, 2172L), X14 = c(0L, 2470L, 442L, 2479L, 
2632L, 1966L, 2102L, 2595L, 2463L, 2227L, 2693L, 1445L, 1588L, 
1559L, 1504L, 2328L, 2344L, 1929L, 2036L, 2367L, 1720L, 2318L, 
1983L, 2066L, 2272L, 1939L, 2101L, 2186L, 2080L, 1981L, 2024L, 
2018L, 2150L, 2131L, 2342L, 2269L, 2431L, 2264L, 2091L, 2344L, 
2047L, 2174L, 2199L), X15 = c(0L, 2278L, 795L, 2485L, 2190L, 
2698L, 1767L, 2670L, 1376L, 2456L, 2520L, 2777L, 2545L, 2207L, 
2425L, 2368L, 1943L, 1946L, 1706L, 1558L, 2201L, 1693L, 2133L, 
2234L, 2279L, 2558L, 1938L, 1896L, 2590L, 2548L, 1705L, 1815L, 
2372L, 1941L, 2037L, 2077L, 2199L, 2353L, 2109L, 2081L, 2099L, 
2010L, 2175L), X16 = c(0L, 1837L, 1656L, 2166L, 1633L, 1599L, 
1539L, 946L, 1481L, 2403L, 1636L, 2344L, 2169L, 2050L, 1830L, 
1814L, 2457L, 2386L, 2065L, 2113L, 2684L, 2135L, 1930L, 2123L, 
1959L, 2303L, 2322L, 2243L, 1698L, 2021L, 1692L, 2421L, 2253L, 
2088L, 1842L, 2147L, 1910L, 1974L, 2326L, 2226L, 2017L, 1996L, 
2163L), X17 = c(0L, 1930L, 1091L, 2436L, 2336L, 2570L, 2327L, 
1246L, 2162L, 2539L, 2280L, 1923L, 2376L, 2402L, 2578L, 2421L, 
2266L, 2298L, 1585L, 2230L, 2731L, 1914L, 2055L, 1859L, 2658L, 
2329L, 2089L, 2096L, 2285L, 1820L, 1816L, 2226L, 2350L, 1974L, 
2264L, 2199L, 2318L, 2243L, 2175L, 1979L, 1896L, 1960L, 2064L
), X18 = c(0L, 2878L, 526L, 502L, 2462L, 2249L, 2485L, 1893L, 
2309L, 2787L, 2137L, 994L, 1887L, 2146L, 1474L, 2408L, 1813L, 
2255L, 2077L, 2123L, 2581L, 2711L, 1965L, 1930L, 2243L, 2251L, 
2129L, 2033L, 2226L, 2021L, 2274L, 1848L, 1816L, 2095L, 2031L, 
2030L, 2007L, 2200L, 2235L, 2336L, 2233L, 2063L, 2168L), X19 = c(0L, 
1563L, 2608L, 2105L, 2514L, 1650L, 2441L, 2334L, 2033L, 1824L, 
1367L, 2799L, 2877L, 1539L, 1707L, 2058L, 2467L, 1929L, 2519L, 
2395L, 2188L, 1820L, 2268L, 2546L, 2216L, 2411L, 2119L, 2058L, 
2246L, 2428L, 2204L, 2253L, 2344L, 2224L, 2101L, 2420L, 2420L, 
1954L, 2175L, 2230L, 2092L, 2061L, 2150L), X20 = c(0L, 2738L, 
442L, 2598L, 368L, 2266L, 2382L, 2106L, 1918L, 2587L, 2815L, 
1146L, 1454L, 2680L, 1549L, 2409L, 2259L, 1937L, 2068L, 2330L, 
2096L, 2454L, 2617L, 1939L, 2134L, 2470L, 2132L, 1922L, 1988L, 
1651L, 2266L, 1816L, 1835L, 2095L, 2342L, 1900L, 2322L, 2256L, 
2109L, 1979L, 2246L, 2077L, 2057L), area = c(0, 83.29863, 333.1945, 
749.68763, 1332.77801, 2082.46564, 2998.75052, 4081.63265, 5331.11204, 
6747.18867, 8329.86256, 10079.13369, 11995.00208, 14077.46772, 
16326.53061, 18742.19075, 21324.44815, 24073.30279, 26988.75469, 
30070.80383, 33319.45023, 36734.69388, 40316.53478, 44064.97293, 
47980.00833, 52061.64098, 56309.87089, 60724.69804, 65306.12245, 
70054.14411, 74968.76302, 80049.97918, 85297.79259, 90712.20325, 
96293.21116, 102040.81633, 107955.01874, 114035.81841, 120283.21533, 
126697.2095, 133277.80092, 140024.98959, 146938.77551), mean = c(0, 
1918.75, 1536.75, 2024.65, 1909.4, 1984.85, 2079.85, 2060.95, 
1970.2, 2118.7, 1992.4, 1969.5, 2112.15, 2052.15, 1945.25, 2124.2, 
2060, 2211.1, 2071.5, 2275.8, 2275.75, 2076.3, 2160.55, 2146.05, 
2211.55, 2252.2, 2054.25, 2171.9, 2121.85, 2108.8, 2068.3, 2023.9, 
2149.5, 2143.8, 2192.4, 2227.7, 2257.85, 2110.85, 2191.2, 2179.35, 
2115.75, 2112.85, 2128.8), sd = c(0, 723.03162, 732.90589, 654.95458, 
793.58662, 580.42065, 542.44527, 567.40884, 483.03314, 538.90748, 
562.7292, 569.32227, 436.96288, 385.95368, 438.89021, 361.63672, 
407.17667, 261.64437, 269.48733, 262.34902, 278.14931, 303.98288, 
219.20322, 214.5876, 232.51723, 227.88746, 197.28496, 179.34938, 
300.51032, 244.39993, 263.49855, 222.17629, 205.11627, 177.62663, 
174.34853, 169.91332, 145.67566, 130.93039, 108.568, 138.17505, 
106.23404, 76.02166, 76.92895)), class = "data.frame", row.names = c(NA, 

How can I solve this error? Thank you in advance

It's unclear what you want to do: if you just want log(mean)~log(area) then you don't need an sapply. If you want to do that for every X1, X2, ... X20 you'd get a single value in each case, it doesn't really make sense to store them in columns that each contain a single value.

You might also want to take a look at the package broom, that is meant to make data.frames with the result of models.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.