简体   繁体   中英

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. 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

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

print(fileData$OGM[1]) returns

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

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

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

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

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. This will get around the stringsAsFactors problem. Or use the approach suggested by 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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