在R中以列的形式在两个数据帧之间应用函数(ks.test)

时间:2017-10-06 11:12:55

标签: r dataframe apply purrr kolmogorov-smirnov

我的简单问题是:如何逐列地在两个数据框之间执行with open(filelocation,"r") as fin: s = set(sys.argv[2:]) for host in s: for line in fin: if host==line.strip(): print line

EG。我们有两个数据框:

ks.test

注意:这只是一个例子 - 真实案例会包含更多列,并且它们包含特定位置中某个元素的浓度。

现在我想在两个数据框之间运行ks.test:

D1 <- data.frame(D$Ag, D$Al, D$As, D$Ba, D$Be, D$Ca, D$Cd, D$Co, D$Cu, D$Cr)
D2 <- data.frame(S$Ag, S$Al, S$As, S$Ba, S$Be, S$Ca, S$Cd, S$Co, S$Cu, S$Cr)

等。如果不做奴隶制工作怎么办呢?

当我在一个数据框上执行shapiro.test时,我只使用:

ks.test(D$Ag, S$Ag)
ks.test(D$Al, S$Al)
ks.test(D$As, S$As)

我已经阅读了一些循环,聚合,mapply的东西 - 尝试了不同的东西,如:

lshap1 <- lapply(D1, shapiro.test)
lres1 <- sapply(lshap1, `[`, c("statistic","p.value"))

然后我得到了很多p值= 0 ..我手动做的情况并非如此。

编辑:09.10.2017 我将数据导入为两个数据帧,然后我将一些数据提取到“较小”的数据帧进行分析 - 例如在这种情况下,查看有毒元素并排除其他元素。

示例数据:apply(D1, 2, function(D2) ks.test(D2,D1[,1])$p.value) dput(head(D1))

dput(head(D2))

我发布这个,因为我仍然收到错误:

## Output dput(head(D1)):
structure(list(DF.As = c(-0.154868225169351, -0.291459578010276,
0.0355227595866723, 0.0892191549433623, 0.189115121672669,
-0.365222418641706
), DF.Cd = c(1.28810277421719, 1.45844987179892, 0.642331353138319,
0.673164023466527, 0.131548822144598, 0.146964746525726), DF.Cu
c(8.01131080231879, 
6.52606822875086, 2.93449454196807, 4.08720148249298, 1.55494291704341,
1.73663851851503), DF.Cr = c(0.164849379809527, 0.196759436988158,
0.307645386162046, 0.302917612808149, 0.187202322026229, 0.25358922601195
), DF.Ni = c(0.362592459542858, 0.527078409257359, 0.477116357433909,
0.469287608844157, 0.225865184678244, 0.355321456594576), DF.Pb
c(0.414448963979605,
0.616598678960665, -0.0531899082482045, 0.47477978516042,
0.422106471495816,
0.0326241032568164), DF.Zn = c(74.7657982668, 74.2978919524635,
36.6575117549406, 47.8440365300156, 21.4962811912273, 23.3823413091772
)), .Names = c("DF.As", "DF.Cd", "DF.Cu", "DF.Cr", "DF.Ni", "DF.Pb",
"DF.Zn"), row.names = c(NA, 6L), class = "data.frame")

## Output dput(head(D2)):
structure(list(DO.As = c(0.0150158517208966, -0.0477743050574027,
-0.121541780066373, -0.0376195600535572, 0.115393920133327,
0.265450918075612), DO.Cd = c(0.367936811743133, 0.445545318262818,
0.350071986298948, 
0.331513644782201, 0.603874629105229, 0.598527030667747), DO.Cu
c(1.65127139067621, 
1.90306634226191, 1.08280240161368, 1.12130376047927, 1.23137174481965,
1.16618813144813), DO.Cr = c(0.162996340978278, 0.493799568371693,
0.18441814919492, 0.179883906525139, 0.128058190333676, 0.030406737049484
), DO.Ni = c(0.290717040452464, 0.331891307317008, 0.387987078391917,
0.36147470695146, 0.774910299821917, 0.323259411199816), DO.Pb
c(-0.0584055598838365, 
0.377799120780818, -0.0741768575020139, 0.511278669452117,
0.320822577941608, 0.250377389869303), DO.Zn = c(16.5625482436821,
14.5084409384572, 16.571001044493, 18.4509635406253, 15.6876446591721,
12.7649440587945)), .Names = c("DO.As", "DO.Cd", "DO.Cu", "DO.Cr", "DO.Ni",
"DO.Pb", "DO.Zn"), row.names = c(NA, 6L), class = "data.frame")

(追溯按钮显示):

## This is code for execution:
col.names = colnames(D1)
lapply(col.names, function(t, d1, d2){ks.test(d1[, t], d2[, t])}, D1, D2)

## Output:
 Error in `[.data.frame`(d2, , t) : undefined columns chosen

2 个答案:

答案 0 :(得分:2)

创建了两个data.frames D1D2,其中包含一些随机数和相同的列名。

set.seed(12)
D1 = data.frame(A=rnorm(n = 30,mean = 5,sd = 2.5),B=rnorm(n = 30,mean = 4.5,sd = 2.2),C=rnorm(n = 30,mean = 2.5,sd = 12))
D2 = data.frame(A=rnorm(n = 30,mean = 5,sd = 2.49),B=rnorm(n = 30,mean = 4.4,sd = 2.2),C=rnorm(n = 30,mean = 2,sd = 12))

现在我们可以使用列名循环并将其传递给D1D2,以便在相应data.frames的相应列上执行ks.test

col.names = colnames(D1)
lapply(col.names,function(t,d1,d2){ks.test(d1[,t],d2[,t])},D1,D2)

#[[1]]

#Two-sample Kolmogorov-Smirnov test

#data:  d1[, t] and d2[, t]
#D = 0.167, p-value = 0.81
#alternative hypothesis: two-sided


#[[2]]

#Two-sample Kolmogorov-Smirnov test

#data:  d1[, t] and d2[, t]
#D = 0.233, p-value = 0.39
#alternative hypothesis: two-sided


#[[3]]

#Two-sample Kolmogorov-Smirnov test

#data:  d1[, t] and d2[, t]
#D = 0.2, p-value = 0.59
#alternative hypothesis: two-sided

在您在问题描述中使用的表示法中,理想情况下,以下代码应该有效:

col.names =colnames(S)
lapply(col.names,function(t,d1,d2){ks.test(d1[,t],d2[,t])},D,S)

答案 1 :(得分:2)

使用purrr软件包中的tidyverse函数和broom软件包中的map函数的tidy解决方案

library(purrr)
library(broom)

# Data posted by @TUSHAr
set.seed(12)
D1 <- data.frame(A = rnorm(n = 30, mean = 5, sd = 2.5), 
                 B = rnorm(n = 30, mean = 4.5, sd = 2.2), 
                 C = rnorm(n = 30, mean = 2.5, sd = 12))
D2 <- data.frame(A = rnorm(n = 30, mean = 5, sd = 2.49), 
                 B = rnorm(n = 30, mean = 4.4, sd = 2.2), 
                 C = rnorm(n = 30, mean = 2, sd = 12))

# Loop through each column
result <- colnames(D1) %>%
  set_names() %>% 
  # apply `ks.test` function for each column pair
  map(~ ks.test(D1[, .x], D2[, .x])) %>%
  # extract test results using `tidy` then bind them together by rows
  map_dfr(., broom::tidy, .id = "parameter")
result

#> # A tibble: 3 x 5
#>   parameter statistic p.value method                           alternative
#>   <chr>         <dbl>   <dbl> <chr>                            <chr>      
#> 1 A             0.167   0.808 Two-sample Kolmogorov-Smirnov t~ two-sided  
#> 2 B             0.2     0.594 Two-sample Kolmogorov-Smirnov t~ two-sided  
#> 3 C             0.233   0.393 Two-sample Kolmogorov-Smirnov t~ two-sided

reprex package(v0.2.0.9000)于2018-08-24创建。