[英]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.