简体   繁体   English

在 R 中读取并转换为数据帧

[英]Reading and converting to data frame in R

I just imported a csv file as a data frame.我刚刚导入了一个 csv 文件作为数据框。 I did this.我这样做了。

myframe <- data.frame(read.csv("afile.csv"))

but when I use the 'typeof()' function on 'myframe', I get a list.但是当我在“myframe”上使用“typeof()”函数时,我得到了一个列表。

> typeof(myframe)
[1] "list"

'myframe' turns out to be a nested list. 'myframe' 原来是一个嵌套列表。 Trying to change the name of the 9th list in the myframe does absolutely nothing.尝试更改 myframe 中第 9 个列表的名称绝对没有任何作用。 I don't even get an error.我什至没有得到错误。 This is my code.这是我的代码。

colnames(myframe[9]) <- "ColName"

I am not able to convert this list to a data frame either (No matter what I do).我也无法将此列表转换为数据框(无论我做什么)。 Any method I try throws up no errors and yet does nothing.我尝试的任何方法都不会出错,但什么也没做。 I tried this.我试过这个。

myframe <- as.data.frame(myframe)

and

myframe  <-  as.data.frame(matrix(unlist(myframe), nrow=length(unlist(myframe[1]))))

(as suggested on a different post) (如另一篇文章所建议)

So this is basically a 4 part question,所以这基本上是一个 4 部分的问题,

  1. Is my R behaving as expected?我的 R 是否按预期运行?
  2. Why isn't an error being returned?为什么不返回错误?
  3. How can change the name of that column?如何更改该列的名称?
  4. How do I convert the nested list to a data frame?如何将嵌套列表转换为数据框?

There are couple of issues in the OP's post. OP 的帖子中有几个问题。

  1. Reading as a data.frame作为 data.frame 读取

    The read.csv gives a data.frame output. read.csv给出了data.frame输出。 So, there is no need to wrap with data.frame ie所以,没有必要用data.frame包裹,即

    myframe <- read.csv("afile.csv")

    If the delimiter is different, use the sep argument and also for non-numeric variables, the default option (because stringsAsFactors=TRUE ) for read.csv will be to return the class as factor .如果分隔符是不同的,使用sep的说法,也非数值变量,默认选项(因为stringsAsFactors=TRUE )为read.csv将返回classfactor In that case, use stringsAsFactors=FALSE .在这种情况下,请使用stringsAsFactors=FALSE

  2. Changing the column names in the dataset.更改数据集中的列名称。 This can be done by selecting the column names that we want to change.这可以通过选择我们想要更改的列名来完成。 As we are changing the 9th column name, select that by indexing and then assign ( <- ) it to new name.当我们更改第 9 列名称时,通过索引选择它,然后将它分配 ( <- ) 给新名称。

     colnames(myframe)[9] <- "ColName"
  3. By checking the typeof(myframe) , it will give "list" as data.frame is a list with length of the list elements equal.通过检查typeof(myframe)它将给"list"作为data.frame是一个listlength的的list元素相等。 If we want to check whether it is a data.frame如果我们要检查它是否是一个data.frame

     is.data.frame(myfile)

    or或者

    str(myframe)
  4. Error message are returned only when there is an error .仅当出现error时才返回错误消息。 Here, it won't give an error as you are wrapping a data.frame by a data.frame call.在这里,当您通过data.frame调用包装data.frame ,它不会出错。 For example例如

    df1 <- data.frame(v1 = 1:5) str(df1) #'data.frame': 5 obs. of 1 variable: #$ v1: int 1 2 3 4 5

    Wrapping with data.frame is not going to change anything except that it is redundant.data.frame包装不会改变任何东西,只是它是多余的。 Of course, we can do data.frame(data.frame(data.frame(df1))) to test as an exercise.当然,我们可以做data.frame(data.frame(data.frame(df1)))来测试作为练习。

     str(data.frame(df1)) #'data.frame': 5 obs. of 1 variable: #$ v1: int 1 2 3 4 5

The default storing of file is a dataframe.文件的默认存储是数据帧。 No need to make it a dataframe.无需将其设为数据框。

    myframe <- read.csv(file.choose(),sep=",",header=T) #Select your file interactively.
    class(myframe)       
    mode(myframe)
    typeof(myframe)
    names(myframe)[9] <- "ColName"   ## Renaming column no. 9 of dataframe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM