简体   繁体   English

如何从 read.csv 生成的数据帧而不是列表中获取单个值?

[英]How can I get a single value from a read.csv-produced dataframe instead of a list?

I am using read.csv on a datapath.我在数据路径上使用 read.csv。 It returns a dataframe.它返回一个数据帧。 I want to be able to get a single value in this dataframe, but instead I get a list of values displaying the levels.我希望能够在此数据框中获得单个值,但我获得了一个显示级别的值列表。

I have tried several ways to access the value I want.我尝试了几种方法来访问我想要的值。 In the next part, I will show you what I tried and the results I got.在下一部分中,我将向您展示我尝试过的方法以及得到的结果。

Here is my simple dataframe:这是我的简单数据框:

"OGM","Nutrient","data3"
"tomato","iron",0.03
"domestic cat","iron",0.02
"zebrafish","zing",0.02
"giraffe","nitrate", 0.09
"common cougar","manganese",0.05
"fawn","nitrogen",0.04
"daim","bromure",0.08
"wild cat","iron",0.05
"domestic cat","calcium",0.02
"muren","calcium",0.07
"jaguar","iron",0.02
"green turtle","sodium",0.01
"dave grohl","metal",0.09
"zebra","nitrates",0.12
"tortoise","sodium",0.16
"dinosaur","calcium",0.08
"apex mellifera","sodium",0.15

Here is how I load the data:这是我加载数据的方式:

   #use read.csv on the datapath contained in file   
   fileData <- read.csv(file[4][[1]])
   print(fileData[1][1])

What I want is to access a single value: from example, "tomato" or "nitrate".我想要的是访问单个值:例如“番茄”或“硝酸盐”。 The result I want is exactly this:我想要的结果正是这样:

>[1] tomato

Here is what I tried and the result I got:这是我的尝试和结果:

print(fileData[1][1]) returns print(fileData[1][1])返回

>  OGM
>1          tomato
>2    domestic cat
>3       zebrafish
>4         giraffe...

print(fileData$OGM[1]) returns print(fileData$OGM[1])返回

>  [1] tomato
Levels: apex mellifera common cougar daim...

print(fileData[1][[1]]) returns print(fileData[1][[1]])返回

> [1] tomato         domestic cat   zebrafish      giraffe        common cougar [...]       
[15] tortoise       dinosaur       apex mellifera
Levels: apex mellifera common cougar daim...

print(fileData$OGM[[1]]) returns print(fileData$OGM[[1]])返回

Levels: apex mellifera common cougar daim...

All apologies for the stupid question, but I'm a bit lost.为这个愚蠢的问题道歉,但我有点迷茫。 All help is appreciated.感谢所有帮助。 If you want me to edit my post to be more clear, tell me.如果你想让我编辑我的帖子更清楚,告诉我。 Thank you.谢谢你。

Some suggestions一些建议

Try readr::read_csv rather than read.csv to read in your data.尝试readr::read_csv而不是read.csv来读入您的数据。 This will get around the stringsAsFactors problem.这将解决stringsAsFactors 问题。 Or use the approach suggested by Stewart Macdonald.或者使用 Stewart Macdonald 建议的方法。

Once you have the data in, you can manipulate it as follows输入数据后,您可以按如下方式操作它

# Make a sample dataframe

library(tidyverse)


df <- tribble(~OGM, ~Nutrient, ~data3,
"tomato","iron",0.03,
"domestic cat","iron",0.02,
"zebrafish","zing",0.02,
"giraffe","nitrate", 0.09,
"common cougar","manganese",0.05,
"fawn","nitrogen",0.04,
"daim","bromure",0.08,
"wild cat","iron",0.05,
"domestic cat","calcium",0.02,
"muren","calcium",0.07,
"jaguar","iron",0.02,
"green turtle","sodium",0.01,
"dave grohl","metal",0.09,
"zebra","nitrates",0.12,
"tortoise","sodium",0.16,
"dinosaur","calcium",0.08,
"apex mellifera","sodium",0.15)

df %>% 
  select(OGM) %>% # select the OGM column
  filter(OGM == 'tomato') %>% 
  pull # convert to a vector

[1] "tomato"

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

相关问题 如何将 R 中的 while 循环生成的单列数据保存到数据帧? - How do I save a single column of data produced from a while loop in R to a dataframe? 如何从 dataframe 中的一行中获取某个值? [R] - How can I get a certain value from a row in dataframe? [R] 如何将列表或数据框中的值添加到 lavaan 语法? - How can I add a value from a list or dataframe to the lavaan syntax? 当 R 中的数据帧列表具有不同的行数时,如何创建单个 dataframe? - How can I create a single dataframe from a list of dataframes in R, when they have different number of rows? 如何导出由 R 生成的 dataframe? - How do I export a dataframe produced by R? 仅当变量等于某个值(20Gb+ csv 文件)时,如何从 csv 文件中读取行 - How can I read lines from a csv file only if a variable equals to a certain value (20Gb+ csv file) 如何从csv获取数据以将其读入R中的函数? - How do I get data from a csv to be read into a function in R? 如何读取 .rdata 文件并从中写入 .csv 文件 - How can I read .rdata file and write .csv files from it 如何让 R 将这些单元格读取为空白而不是 NA? - How can I get R to read these cells as blank instead of NA? 我如何获取R中的列A值等于列B值的数据帧的子集(从CSV读取的数据) - How do i get subset of a data frame where Column A value equals Column B value in R ( Data read from CSV)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM