在R中更改df的列类

时间:2018-04-18 10:52:03

标签: r

我从空<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".uiFragments.WikiFragment"> <RelativeLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingTop="50dp"> <TextView android:id="@+id/ampel_weiss" android:layout_width="60dp" android:layout_height="60dp" android:layout_marginTop="5dp" android:layout_marginRight="5dp" android:gravity="center" android:textSize="40sp" android:text="TEST" android:textColor="#00000C" android:background="@color/ampelWE20" android:layout_alignParentLeft="true"/> <ListView android:id="@+id/custom_list" android:layout_height="100dp" android:layout_width="1000dp" android:listSelector="@drawable/list_color_selector" android:background="@drawable/listview_border" android:layout_centerHorizontal="true"/> </RelativeLayout> </FrameLayout> 开始。

df

现在我以下列方式向此 > df <- data.frame("coeff" = numeric(), "value" = numeric(), "Type" = character()) 添加了几行。

df

注意:> df<- rbind(df,c(0.05,sum(main$c1), "BaseLine"), stringsAsFactors = FALSE ) 是另一个main,此处添加了col df需求的总和。 (到目前为止,没有什么可担心的程序)。使用此方法,我向c1添加了一些行。由于此代码中的最后一个字段为df,因此character将所有列转换为rbind

我有两个问题。

  1. 是否可以使用character,而不是rind中所述的hierarchy: raw < logical < integer < double < complex < character < list

  2. 有没有办法将所有数字类转换为字符并在字符类中保留值而不插入rbind help
    例如。使用时:
    NAs > sapply(df, as.numeric)Type列中的值已更改为df

  3. 我正在寻找一些函数,它可以将包含数字的字符字段转换为数字,同时保留包含字符的字符字段。

      

    我不想使用:
      df $ coeff&lt; - as.numeric(df $ coeff)
      df $ value&lt; - as.numeric(df $ value)
      因为我有很多列,每次读取NAs的输入时列数都会发生变化。

1 个答案:

答案 0 :(得分:1)

问题是c(0.05,sum(main$c1), "BaseLine")会返回character向量。

尝试:

df <- data.frame("coeff" = numeric(), "value" = numeric(), "Type" = character(), stringsAsFactors = FALSE)
df <- rbind.data.frame(df,cbind.data.frame(0.05, 100, "BaseLine", stringsAsFactors = FALSE), stringsAsFactors = FALSE )
> str(df)
'data.frame':   1 obs. of  3 variables:
 $ 0.05      : num 0.05
 $ 100       : num 100
 $ "BaseLine": chr "BaseLine"