我的数据框如下所示:
Name Value Value1 C1 C2 C3
A 1 2 NA NA NA
A NA 2 NA 2 NA
A 1 2 NA 2 NA
A 1 2 NA 2 NA
A 1 2 NA 2 NA
B NA 1 NA 2 NA
B NA 2 NA 2 NA
B 1 NA NA 2 NA
B 1 NA NA 2 NA
C 1 5 NA 2 NA
C 1 5 NA 2 NA
dt <- as.data.table(df)
new <- dt[is.na(`Value`) == FALSE & is.na(`Value1`) == FALSE,]
会给我以下输出:
Name Value Value1 C1 C2 C3
A 1 2 NA NA NA
A 1 2 NA 2 NA
A 1 2 NA 2 NA
A 1 2 NA 2 NA
C 1 5 NA 2 NA
C 1 5 NA 2 NA
有没有办法做到这一点,但我希望我的列Name中的所有字母都不符合我的过滤条件,而不是过滤行。 所以在这个例子中,我想回到A和B,因为A有一行没有两列Value和Value1非na值,B根本不符合标准。
答案 0 :(得分:2)
您可以使用!
作为否定运算符。 (事实上,您应该已经使用它而不是== FALSE
。此外,除非您的列名很奇怪(它们有空格或其他内容),否则您不需要反引号。
# your code, rewritten with ! instead of == FALSE
df[!is.na(Value) & !is.na(Value1), ]
# negate it to get the opposite
# same as above, but with !( your logic )
df[!(!is.na(Value) & !is.na(Value1)), ]
Name Value Value1 C1 C2 C3
1: A NA 2 NA 2 NA
2: B NA 1 NA 2 NA
3: B NA 2 NA 2 NA
4: B 1 NA NA 2 NA
5: B 1 NA NA 2 NA
答案 1 :(得分:2)
我可能误解了你,但就我可以阅读说明而言,我认为你的意思是获得特定Name
的所有行,其中包含至少一个NA
。
可以使用ave
来完成此操作,我们会检查NA
或Value
列中是否至少有一个Value1
,然后选择整个组Name
})。
df[with(df, ave(is.na(Value) | is.na(Value1), Name, FUN = any)), ]
# Name Value Value1 C1 C2 C3
#1 A 1 2 NA NA NA
#2 A NA 2 NA 2 NA
#3 A 1 2 NA 2 NA
#4 A 1 2 NA 2 NA
#5 A 1 2 NA 2 NA
#6 B NA 1 NA 2 NA
#7 B NA 2 NA 2 NA
#8 B 1 NA NA 2 NA
#9 B 1 NA NA 2 NA
答案 2 :(得分:1)
@RonakShah已经指出(使用public class GridWidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new
NewsListRemoteVieFactory(this.getApplicationContext());
}
}
class NewsListRemoteVieFactory implements
RemoteViewsService.RemoteViewsFactory{
public static ArrayList<News> newsArrayList = new ArrayList<>();
Context mContext;
public NewsListRemoteVieFactory(Context applicationContext) {
this.mContext = applicationContext;
}
private void readNews(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext.getApplicationContext());
Gson gson = new Gson();
String json = prefs.getString("news", "");
Type type = new TypeToken<ArrayList<News>>(){}.getType();
newsArrayList = gson.fromJson(json, type);
}
@Override
public void onCreate() {
readNews();
}
@Override
public void onDataSetChanged() {
readNews();
}
@Override
public void onDestroy() {
}
@Override
public int getCount() {
if(newsArrayList == null){
return 0;
}
return newsArrayList.size();
}
@Override
public RemoteViews getViewAt(int position) {
RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.widget_grid_view_item);
views.setTextViewText(R.id.widget_grid_view_item, "\u2022 " + newsArrayList.get(position).getTitle()
+ "\n" + String.valueOf(newsArrayList.get(position).getDescription()));
Bundle selectedNewsBundle = new Bundle();
selectedNewsBundle.putParcelableArrayList("news",newsArrayList);
Intent fillInIntent = new Intent();
views.setOnClickFillInIntent(R.id.widget_grid_view_item, fillInIntent);
//PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, fillInIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//views.setOnClickPendingIntent(R.id.widget_grid_view_item, pendingIntent);
Intent intent = new Intent(mContext, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
views.setOnClickPendingIntent(R.id.widget_grid_view_item, pendingIntent);
return views;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean hasStableIds() {
return true;
}
}
)如何base-R
filter
Name
行NA
或Value
中至少有Value1
行{1}}列。
但是,或许,OP正在寻找仅unique
个名称而不是完整的行子集。在dplyr
中,可以这样做:
library(dplyr)
df %>% group_by(Name) %>%
filter_at(vars(starts_with("Value")), any_vars(is.na(.))) %>% #Either Value or Value1
select(Name) %>% distinct()
# Name
# <chr>
# 1 A
# 2 B
要获取Name
或NA
或Value
Value1
df %>% group_by(Name) %>%
filter(any(is.na(Value) | any(is.na(Value1))))
# Name Value Value1 C1 C2 C3
# <chr> <int> <int> <lgl> <int> <lgl>
# 1 A 1 2 NA NA NA
# 2 A NA 2 NA 2 NA
# 3 A 1 2 NA 2 NA
# 4 A 1 2 NA 2 NA
# 5 A 1 2 NA 2 NA
# 6 B NA 1 NA 2 NA
# 7 B NA 2 NA 2 NA
# 8 B 1 NA NA 2 NA
# 9 B 1 NA NA 2 NA
的完整行
df <- read.table(text =
"Name Value Value1 C1 C2 C3
A 1 2 NA NA NA
A NA 2 NA 2 NA
A 1 2 NA 2 NA
A 1 2 NA 2 NA
A 1 2 NA 2 NA
B NA 1 NA 2 NA
B NA 2 NA 2 NA
B 1 NA NA 2 NA
B 1 NA NA 2 NA
C 1 5 NA 2 NA
C 1 5 NA 2 NA",
header = TRUE, stringsAsFactors = FALSE)
数据:强>
public