如何从 read.csv 生成的数据框而不是列表中获取单个值?
How can I get a single value from a read.csv-produced dataframe instead of a list?
我在数据路径上使用 read.csv。它 returns 一个数据框。我希望能够在此数据框中获得单个值,但我得到的是显示级别的值列表。
我已经尝试了几种方法来访问我想要的值。在下一部分中,我将向您展示我的尝试以及我得到的结果。
这是我的简单数据框:
"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
这是我加载数据的方式:
#use read.csv on the datapath contained in file
fileData <- read.csv(file[4][[1]])
print(fileData[1][1])
我想要的是访问单个值:例如,"tomato" 或 "nitrate"。我想要的结果就是这样:
>[1] tomato
这是我的尝试和得到的结果:
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...
对于这个愚蠢的问题,我深表歉意,但我有点迷茫。感谢所有帮助。如果你想让我编辑我的 post 更清楚,请告诉我。谢谢。
一些建议
尝试 readr::read_csv
而不是 read.csv
来读入您的数据。这将解决 stringsAsFactors 问题。或者使用 Stewart Macdonald 建议的方法。
输入数据后,您可以按如下方式操作它
# 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"
我在数据路径上使用 read.csv。它 returns 一个数据框。我希望能够在此数据框中获得单个值,但我得到的是显示级别的值列表。
我已经尝试了几种方法来访问我想要的值。在下一部分中,我将向您展示我的尝试以及我得到的结果。
这是我的简单数据框:
"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
这是我加载数据的方式:
#use read.csv on the datapath contained in file
fileData <- read.csv(file[4][[1]])
print(fileData[1][1])
我想要的是访问单个值:例如,"tomato" 或 "nitrate"。我想要的结果就是这样:
>[1] tomato
这是我的尝试和得到的结果:
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...
对于这个愚蠢的问题,我深表歉意,但我有点迷茫。感谢所有帮助。如果你想让我编辑我的 post 更清楚,请告诉我。谢谢。
一些建议
尝试 readr::read_csv
而不是 read.csv
来读入您的数据。这将解决 stringsAsFactors 问题。或者使用 Stewart Macdonald 建议的方法。
输入数据后,您可以按如下方式操作它
# 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"