使用R从数据框中删除NA值

时间:2016-11-18 01:24:48

标签: r

我有一个大型数据框?_?维度为501行和42844列的值。使用R,我已经使用以下代码替换为NA:

data[data == "?_?"] <- NA

所以我现在有NA值,我想从Data.frame中省略这些值,但有些事情变坏了.... 当我按下面的命令时:

data_na_rm <- na.omit(data)

我得到一个0,42844对象。

dim(data_na_rm) #gives me 0 42844
data_na_rm[1,2] #gives me NA
data_na_rm[5,3] #gives me NA
############################
data_na_rm[2]   #gives me the title of the second column 
data_na_rm[5]   #gives me the title fo the fifth

我该怎么办?我花了很多时间在这件事上。如果有人能花一些时间来帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

就像JackStat在评论中所说的那样,你可能每行都有NAs。也许你应该测试一下?:

apply(df, 2, function(x) {sum(is.na(x))})
col1 col2 col3 col4 col5 
   1    1    1    1    0

这将帮助您找到哪些列贡献最多的NA。它总结了NAs的数量:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:background="@color/colorAccent"
        android:id="@+id/textView"
        android:text="1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintRight_toLeftOf="@+id/textView2"
        app:layout_constraintDimensionRatio="1:1" />

    <TextView
        android:background="@color/colorAccent"
        android:id="@+id/textView2"
        android:text="2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintRight_toLeftOf="@+id/textView3"
        app:layout_constraintLeft_toRightOf="@+id/textView"
        app:layout_constraintDimensionRatio="1:1" />

    <TextView
        android:background="@color/colorAccent"
        android:id="@+id/textView3"
        android:text="3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintRight_toLeftOf="@+id/textView4"
        app:layout_constraintLeft_toRightOf="@+id/textView2"
        app:layout_constraintDimensionRatio="1:1" />

    <TextView
        android:background="@color/colorAccent"
        android:id="@+id/textView4"
        android:text="4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintRight_toLeftOf="@+id/textView5"
        app:layout_constraintLeft_toRightOf="@+id/textView3"
        app:layout_constraintDimensionRatio="1:1" />

    <TextView
        android:background="@color/colorAccent"
        android:id="@+id/textView5"
        android:text="5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/textView4"
        app:layout_constraintDimensionRatio="1:1" />

</android.support.constraint.ConstraintLayout>