我有一个数据框操作问题。
我想找到数据框的子集" data1"每个col的总和等于另一个数据帧" data2"。
这是我的代码:
public byte[] saveImage() {
//reder the view once, in software.
buildDrawingCache();
//get the rendered bitmap
Bitmap bitmap = getDrawingCache();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100 /*ignored for PNG*/, bos);
byte[] data = bos.toByteArray();
//destroy the drawing cache, to clear up the memory
destroyDrawingCache();
try {
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
if (data == null) {
Log.e(TAG, "data in touchview before save clicked is null");
} else {
Log.e(TAG, "data in touchview before saved clicked is not null and has length + " + data.length);
}
return data;
}
我想知道其他任何算法是否有帮助?
谢谢!
答案 0 :(得分:2)
此解决方案仅考虑您要从任意两行计算总和的方案。如果要测试其他行号,则需要通过更改combn
函数中的数字来创建这些组合。 final_data
是最终输出。如果有多个匹配项,您可能希望将final_data
保留为列表。
# Prepare example datasets
AA<-c(2,3,1,4,9)
BB<-c(5,13,9,1,2)
A1<-c(5)
B1<-c(18)
data1<-data.frame(AA,BB)
data2<-data.frame(A1,B1)
# Load packages
library(tidyverse)
# Use combn to find out all the combination of row number
row_indices <- as.data.frame(t(combn(1:nrow(data1), 2)))
# Prepare a list of data frame. Each data frame is one row from row_indices
row_list <- row_indices %>%
rowid_to_column() %>%
split(f = .$rowid)
# Based on row_list to subset data1
sub_list <- map(row_list, function(dt){
temp_data <- data1 %>% filter(row_number() %in% c(dt$V1, dt$V2))
return(temp_data)
})
# Calcualte the sum of each data frame in sub_list
sub_list2 <- map(sub_list, function(dt){
dt2 <- dt %>%
summarise_all(funs(sum(.))) %>%
setNames(c("A1", "B1"))
return(dt2)
})
# Compare each data frame in sub_list2 with data2
# Find the one that is the same and store the logical results in result_indices
result_indices <- map_lgl(sub_list2, function(dt) setequal(dt, data2))
# Get the final output
final_data <- sub_list[result_indices][[1]]
final_data
AA BB
1 2 5
2 3 13