我有一个具有2行30406列的大型数据框。我需要计算给定列(匹配)的两行中出现0的次数以及给定列中另一行而不是另一行中出现0的次数(不匹配)。
我认为,如果我仅遍历所有内容并比较每列,那么考虑到> 30k列,这将花费很长时间
UINavigationBar.appearance(whenContainedInInstancesOf: [QLPreviewController.self]).isTranslucent = false
UINavigationBar.appearance(whenContainedInInstancesOf: [QLPreviewController.self]).tintColor = colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
UINavigationBar.appearance(whenContainedInInstancesOf: [QLPreviewController.self]).titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance(whenContainedInInstancesOf: [QLPreviewController.self]).setBackgroundImage(UIImage.init(color: colorLiteral(red: 0.9003542662, green: 0.4175613821, blue: 0.1471666098, alpha: 1)), for: .default)
present(quickLookController, animated: true, completion: nil)
输出
head(to_compare)[1:5]
bin:82154:182154 bin:82154:282154 bin:82154:382154
bin:82154:482154
1-D1.txt 0 1 2
0
1-D2.txt 1 1 1
1
bin:82154:582154
1-D1.txt 0
1-D2.txt 0
答案 0 :(得分:3)
您可以将colSums
用于矢量化解决方案:
set.seed(123)
df <- as.data.frame(matrix(round(runif(50, 0, 2)), nrow = 2))
# Match
sum(colSums(df==0) == 2)
[1] 2
# No match
sum(colSums(df==0) == 1)
[1] 8
答案 1 :(得分:1)
<input type="text" name="password" value="" id="password" />
<button id="bocken">ok.</button>
</form>
<div id="result"></div>
答案 2 :(得分:1)
另一种非常简单的方法是先将列切换为行,然后使用rowSums
:
#Create sample df
df <- data.frame(col1 = c(0,1), col2 = c(1,0), col3 = c(1,1), col4 = c(0,2), col5 = c(3,0), col6 = c(0,0))
#Convert columns to rows
df_long <- t(df)
#Count number of 0s in every row and show in table of 0, 1 or 2 zeros
table(rowSums(df_long == 0))
0 1 2
1 4 1
答案 3 :(得分:0)
set.seed(7)
n <- 30406
to_compare <- data.frame(matrix(floor(runif(n*2, 0, 3)), nrow = 2))
table(colSums(to_compare==0))
# 0 1 2
#13519 13513 3374
#
#0..no zero in column (13519)
#1..one row in column has a zero (13513)
#2..both rows in column are zero (3374)
system.time(table(colSums(to_compare==0)))
# User System verstrichen
# 0.332 0.000 0.330