【问题标题】:Passing a String Via Index in R通过 R 中的索引传递字符串
【发布时间】:2013-12-10 15:18:07
【问题描述】:

我正在尝试使用 for 循环从带有 R 的数据框中提取数据子集。

我有一个小向量来保存该列中所有可能出现的名称

meter_class<-c("one_s_120","nine_s_120", "nine_s_480","fortyfive_s_120", "fortyfive_s_480")

每当我尝试通过索引引用来解决它时,它都会失败。要么数据子集中没有任何东西存在(到处都是 NULL),要么 R 抱怨没有通过使用 meter_class[1]

传递正确的参数
attach(meter_class[1])
Error in attach(meter_class[1]) : file 'one_s_120' not found
subset(cal, cal$Form==as.character(meter_class[1]))
[1] Test     Amps     Type     Accuracy Voltage  Form 
<0 rows> (or 0-length row.names)

另外,这是dput 在数据帧cal 上的输出:

    structure(list(Test = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("Burst", "ESD", "Inspection", 
"Surge"), class = "factor"), Amps = c(15, 15, 1.5, 2.5, 2.5, 
0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 15, 15, 
1.5, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 
0.25, 15, 15, 1.5, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 
0.25, 2.5, 2.5, 0.25, 15, 15, 1.5, 2.5, 2.5, 0.25, 2.5, 2.5, 
0.25, 2.5, 2.5, 0.25, 2.5, 2.5, 0.25), Type = structure(c(2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 
3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 
2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L), .Label = c("Lag - 0.5", 
"Unity - Full", "Unity - Light"), class = "factor"), Accuracy = c(-0.011, 
0.012, 0.027, 0.033, 0.076, 0.006, 0.052, 0.046, -0.016, 0.021, 
0.008, 0.023, 0.034, 0.036, 0.038, 0.002, 0.012, 0.097, 0.055, 
0.093, 0.033, 0.068, 0.048, -0.016, 0.042, 0.03, 0.035, 0.041, 
0.024, 0.027, 0.004, -0.012, 0.002, 0.038, 0.084, 0.015, 0.049, 
0.045, -0.009, 0.025, 0.002, 0.029, 0.03, 0.032, 0.064, 0.011, 
0.024, 0.033, 0.054, 0.085, 0.027, 0.071, 0.059, 0.01, 0.051, 
0.012, 0.051, 0.048, 0.04, 0.051), Voltage = c(120, 120, 120, 
120, 120, 120, 480, 480, 480, 120, 120, 120, 480, 480, 480, 120, 
120, 120, 120, 120, 120, 480, 480, 480, 120, 120, 120, 480, 480, 
480, 120, 120, 120, 120, 120, 120, 480, 480, 480, 120, 120, 120, 
480, 480, 480, 120, 120, 120, 120, 120, 120, 480, 480, 480, 120, 
120, 120, 480, 480, 480), Form = structure(c(3L, 3L, 3L, 2L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("45S", "9S", "i210plus"
), class = "factor")), .Names = c("Test", "Amps", "Type", "Accuracy", 
"Voltage", "Form"), class = "data.frame", row.names = c(NA, -60L
))

如果你知道怎么做,我知道这是一件很简单的事情......谁能照亮道路?

谢谢!

【问题讨论】:

  • 您好,欢迎来到 SO!如果您发布 minimal, reproducible example,人们会更容易帮助您解决问题。此外,发布任何错误消息 - 它们通常非常有用。最后,避免使用attach。参见例如?attach,"Good practice",以及论坛中关于attach 可能引起的不愉快意外的几个主题。
  • 感谢您的回复!我听取了您的建议并更新了代码以包含错误消息。
  • 帖子已更新。感谢您提供帮助。
  • 您在cal 中的Form 列只有两个条目:i210plus 9S 和45S。它们都不在您的向量meter_class 中。但是,您使用的语法几乎是正确的(不惜一切代价避免使用attach!)cal[cal$Form %in% meter_class, ]。
  • 好的,我看到了一些问题。我继续将meter_class更改为c(“i210plus”,“9S”,“45S”)。因此,在不确切知道贾斯汀对 cal[cal$Form %in%meter_class, ] 的评论对 %in% 术语的作用的情况下,让我问你这个问题:你将如何使用该语法来选择所有的 9S 类仪表以 120 而不是 480 运行?你能以某种方式在其中插入一个 AND 吗? :) 谢谢...这个网站非常有用和信息丰富!

标签: r class types indexing character


【解决方案1】:

您的数据框中的“表单”中似乎没有任何“meter_class”的值。

unique(df$Form)
# [1] i210plus 9S       45S

meter_class %in% unique(df$Form)
# [1] FALSE FALSE FALSE FALSE FALSE

只需尝试两种形式的子集,使用数据中实际存在的“Form”值:

subset(df, Form == "9S")
df[df$Form == "9S", ]

我还注意到您希望“从数据框中提取数据子集”。不知道完整的故事和您这样做的目标,但请注意,有很多函数可以让您对数据的子集执行计算、绘图或其他操作。

更新以下评论
您可以通过将逻辑条件与逻辑运算符组合来对数据框进行子集化(参见例如?Extract、?&amp;)

meter_class <- c("i210plus", "9S", "45S")
df[df$Form == "9S" & df$Voltage == 120, ]
# or
subset(df, Form == "9S" & Voltage == 120)

【讨论】:

    猜你喜欢
    • 2014-08-11
    • 2017-07-25
    • 2016-07-05
    • 2010-11-21
    • 2010-12-24
    • 2010-11-19
    • 2018-12-07
    • 2018-09-01
    • 2013-04-07
    相关资源
    最近更新 更多