R 两种相同类型的数据帧在一个函数中执行不同的操作
下面是我的数据R 两种相同类型的数据帧在一个函数中执行不同的操作,r,R,下面是我的数据 set.seed(100) toydata <- data.frame(A = sample(1:50,50,replace = T), B = sample(1:50,50,replace = T), C = sample(1:50,50,replace = T) ) 在第二种情况下,我通过分别交换每个列来获得新数据toy。代码如下: toydata<-as.data.fra
set.seed(100)
toydata <- data.frame(A = sample(1:50,50,replace = T),
B = sample(1:50,50,replace = T),
C = sample(1:50,50,replace = T)
)
在第二种情况下,我通过分别交换每个列来获得新数据toy
。代码如下:
toydata<-as.data.frame(toydata)
toy2 <- toydata # Work with a copy
toy2[] <- lapply(toydata, swapFun)
toy<-toy2
然而,在这两种情况下,toy
数据都是具有相同维度、相同行名和相同列名的数据帧。我感到困惑
我怎样才能修好它?谢谢。在第一种情况下,在我添加
toydataHi之后,在执行toydataHi之后,您执行toydataHi,现在是另一个错误。由于某些原因,您制作的表(表1和表2)没有相同的尺寸。是的,您是对的!谢谢
toydata<-as.matrix(toydata)
toy<-swapFun(toydata)
toy<-as.data.frame(toy)
toydata<-as.data.frame(toydata)
toy2 <- toydata # Work with a copy
toy2[] <- lapply(toydata, swapFun)
toy<-toy2
# the function to compare contingency tables
f = function(x,y){
table1<-table(toydata[,x],toydata[,y])
table2<-table(toy[,x],toy[,y])
sum(abs(table1-table2))
}
# vectorise your function
f = Vectorize(f)
combn(x=names(toydata),
y=names(toydata), 2) %>%# create all combinations of your column names
t() %>% # transpose
data.frame(., stringsAsFactors = F) %>% # save as dataframe
filter(X1 != X2) %>% # exclude pairs of same
# column
mutate(SumAbs = f(X1,X2)) # apply function
+ filter(X1 != X2) %>% # exclude pairs of same column
+ mutate(SumAbs = f(X1,X2)) # apply function
Error in combn(x = names(toydata), y = names(toydata), 2) : n < m