[R] Binding result of a function to a data frame

jim holtman jholtman at gmail.com
Tue Jun 24 04:53:08 CEST 2008


is this what you are looking for:

> x <- read.table(textConnection("                                   V1             V2      V3      V4      V5
+ 1                       1007_s_at           DDR1  2865.1  2901.3  1978.3
+ 2                         1053_at           RFC2   103.6    81.6   108.0
+ 3                          117_at          HSPA6   222.0   222.5   306.2
+ 4                          121_at           PAX8  2146.3  2457.2  1901.9
+ 5                       1255_g_at         GUCA1A   226.5   321.2   109.2
+ 6                         1294_at          UBE1L   296.6   424.6   406.1
+ 7                         1316_at           THRA   339.9   354.2   178.3
+ 8                         1320_at         PTPN21    78.4    36.2
185.2"), header=TRUE)
> closeAllConnections()
> kurtosis <-function(x) (mean((x-mean(x))^4))/(sd(x)^4)  #x is a vector
> cbind(x, kurtosis=apply(x, 1, function(z) kurtosis(as.numeric(unlist(z[3:5])))))
         V1     V2     V3     V4     V5  kurtosis
1 1007_s_at   DDR1 2865.1 2901.3 1978.3 0.6666667
2   1053_at   RFC2  103.6   81.6  108.0 0.6666667
3    117_at  HSPA6  222.0  222.5  306.2 0.6666667
4    121_at   PAX8 2146.3 2457.2 1901.9 0.6666667
5 1255_g_at GUCA1A  226.5  321.2  109.2 0.6666667
6   1294_at  UBE1L  296.6  424.6  406.1 0.6666667
7   1316_at   THRA  339.9  354.2  178.3 0.6666667
8   1320_at PTPN21   78.4   36.2  185.2 0.6666667
>


On Mon, Jun 23, 2008 at 10:31 PM, Gundala Viswanath <gundalav at gmail.com> wrote:
> Hi,
>
> I have the following function:
>
>> kurtosis <-function(x) (mean((x-mean(x))^4))/(sd(x)^4)  #x is a vector
>
> and data
>
>> print(mydata)
>                                    V1             V2      V3      V4      V5
> 1                       1007_s_at           DDR1  2865.1  2901.3  1978.3
> 2                         1053_at           RFC2   103.6    81.6   108.0
> 3                          117_at          HSPA6   222.0   222.5   306.2
> 4                          121_at           PAX8  2146.3  2457.2  1901.9
> 5                       1255_g_at         GUCA1A   226.5   321.2   109.2
> 6                         1294_at          UBE1L   296.6   424.6   406.1
> 7                         1316_at           THRA   339.9   354.2   178.3
> 8                         1320_at         PTPN21    78.4    36.2   185.2
>
> Now, I want to:
> 1.  apply the kurtosis function to every row in the data (starting
> from V3 to V5)
> 2.  bind the output of the kurtosis function to mydata.
>
> Why my code below doesn't work? What's the solution for it?
>
>  binded_mydata_withkurt <- cbind(mydata,
> kurt=apply(mydata,1,kurtosis(mydata[,3:ncol(repo.dat)]),na.rm = TRUE))
>
>
> - Gundala Viswanath
> Jakarta - Indonesia
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list