从数据框中提取多个但特定的值

时间:2019-04-27 15:05:39

标签: r

我正在尝试从数据框中定期绘制特定值,因为它给了我This Plot,但是我想要的图是This one;但是,我似乎无法找到正确的语法来实现它。我使用的代码给了我:

Error in Food$Ár[1, 5, 9, 11] : incorrect number of dimensions 

代码:

plot(x = Food$Ár[1,5,9,11], y = Food$Yngri.en.67.ára.Fjöldi[1,5,9,11], type = "o")

1 个答案:

答案 0 :(得分:0)

如果您想对特定的行进行子集化(通过数字知道行索引),[]将起作用。

dat <- data.frame(list(
x = sample(c("Apple","Orange","Banana","Chicken","Fish","Tomato"),
         size = 12, replace = T),
y = sample(c("Sweet","Savory","Spicy","Salty"),
         size = 12, replace = T)))

head(dat)
        x      y
1  Orange Savory
2    Fish  Salty
3 Chicken  Spicy
4   Apple  Spicy
5  Banana  Sweet
6 Chicken  Salty

#To select rows 1, 5, 9, and 11:
dat[c(1,5,9,11), ]
         x      y
1   Orange Savory
5   Banana  Sweet
9  Chicken  Spicy
11  Orange Savory