当前位置: 动力学知识库 > 问答 > 编程问答 >

r - Include item number when converting list to dataframe

问题描述:

I'm trying to convert a list of items to a data frame and keep the item numbers with the new rows in the data frame.

I want to keep the first 6 rows of data for 1800 specimens but the data is returned as a list. When I convert the list to a data frame, I lose track of which rows came from which items due to some items not returning any data during the batch process, thus, not showing up in the new data frame. I pasted the data from the first 6 items in the list below.

-My workflow is as follows:

out <-lapply(out, head)

out

[[1]]

ID sequencedescription database citation

1 MNAG563-08 COI-5P BOLD: Public Records BOLD Systems, 2016

2 MNAG564-08 COI-5P BOLD: Public Records BOLD Systems, 2016

3 IAWL696-09 COI-5P BOLD: Public Records BOLD Systems, 2016

4 LPOKD816-10 COI-5P BOLD: Public Records BOLD Systems, 2016

5 GMEM065-11 COI-5P BOLD: Public Records BOLD Systems, 2016

6 GMGSA075-12 COI-5P BOLD: Public Records BOLD Systems, 2016

taxonomicidentification similarity

1 Lepidoptera 1

2 Lepidoptera 1

3 Lepidoptera 1

4 Lepidoptera 1

5 Lepidoptera 1

6 Lepidoptera 1

specimen_country specimen_lat specimen_lon

1 United States 39.717 -78

2 United States 38.991 -77.235

3 United States 30.08 -97.167

4 United States 36.74 -95.95

5 United States 33.4156 -89.2606

6 United States 35.6859 -83.4986

...

[[5]]

ID sequencedescription database citation

1 MEC818-04 COI-5P BOLD: Public Records BOLD Systems, 2016

2 RDLQE057-06 COI-5P BOLD: Public Records BOLD Systems, 2016

3 LNCB034-06 COI-5P BOLD: Public Records BOLD Systems, 2016

4 RDLQI742-09 COI-5P BOLD: Public Records BOLD Systems, 2016

5 RDLQI756-09 COI-5P BOLD: Public Records BOLD Systems, 2016

6 BBLSW607-09 COI-5P BOLD: Public Records BOLD Systems, 2016

taxonomicidentification similarity

1 Lepidoptera 1

2 Lepidoptera 1

3 Lepidoptera 1

4 Lepidoptera 1

5 Lepidoptera 1

6 Lepidoptera 1

specimen_country specimen_lat specimen_lon

1 Canada 45.3967 -75.849

2 Canada 45.465 -73.075

3 United States 34.768 -76.764

4 Canada 49.2417 -72.423

5 Canada 45.4998 -76.3522

6 United States 33.883 -96.821

[[6]]

NULL

-Then I convert to a dataframe

out_frame <- do.call("rbind", lapply(out, data.frame))

-Then write to an xlsx file and first tried to assign ID #1 to the first 6 rows, ID #2 to the next 6 rows, etc. but it doesn't line up since item 6 returns NULL.

网友答案:

Here's a solution using a list of data frames:

out <- list(
  data.frame(a=10:30, b=20:40),
  data.frame(a=10:50, b=20:60),
  NULL
)

out2 <- lapply(seq(along=out), function(i) {
  df <- out[[i]]
  if(!is.null(df)) df$ID <- i
  df
})

out_frame <- do.call(rbind, lapply(out2, head))

out_frame
网友答案:

Thank you for these replies. I was able to try several things and found that setting stringsAsFactors = FASLE when reading in data and then using the setNames function to assign my original IDs before requesting the output worked great. Then converting the output to a dataframe and writing to xlsx had the original IDs retained in the first column.

分享给朋友:
您可能感兴趣的文章:
随机阅读: