在R中提取csv文件的特定观察结果

时间:2014-07-29 00:25:12

标签: r csv dataframe

我使用read.csv导入了一个csv文件。 它给了我一个18k观察1变量的数据框,如下所示:

    V1
1   Energies (kJ/mol)
2              Bond          Angle    Proper Dih.  Improper Dih.          LJ-14
3       3.12912e+04    4.12307e+03    1.63677e+04    1.25619e+02    1.04394e+04
4        Coulomb-14        LJ (SR)   Coulomb (SR)      Potential Pressure (bar)
5       9.21339e+04    2.82339e+05   -1.15807e+06   -7.21252e+05   -7.25781e+03
6              Step           Time         Lambda
7                 1        1.00000        0.00000
8      Energies (kJ/mol)
9              Bond          Angle    Proper Dih.  Improper Dih.          LJ-14
10      2.71553e+04    4.11858e+03    1.63855e+04    1.22226e+02    1.03903e+04
11       Coulomb-14        LJ (SR)   Coulomb (SR)      Potential Pressure (bar)
12      9.20926e+04    2.65253e+05   -1.15928e+06   -7.43766e+05   -7.27887e+03
13             Step           Time         Lambda
14                2        2.00000        0.00000

...

我想在矢量中提取势能。我尝试过多种类型和功能的grep和readLines,但没有任何作用。有人知道如何解决这个问题吗? 谢谢! :)

1 个答案:

答案 0 :(得分:0)

这是一个正确的答案(来自前fizzsics专业):

Lines <- readLines(textConnection("1   Energies (kJ/mol)
2              Bond          Angle    Proper Dih.  Improper Dih.          LJ-14
3       3.12912e+04    4.12307e+03    1.63677e+04    1.25619e+02    1.04394e+04
4        Coulomb-14        LJ (SR)   Coulomb (SR)      Potential Pressure (bar)
5       9.21339e+04    2.82339e+05   -1.15807e+06   -7.21252e+05   -7.25781e+03
6              Step           Time         Lambda
7                 1        1.00000        0.00000
8      Energies (kJ/mol)
9              Bond          Angle    Proper Dih.  Improper Dih.          LJ-14
10      2.71553e+04    4.11858e+03    1.63855e+04    1.22226e+02    1.03903e+04
11       Coulomb-14        LJ (SR)   Coulomb (SR)      Potential Pressure (bar)
12      9.20926e+04    2.65253e+05   -1.15928e+06   -7.43766e+05   -7.27887e+03
13             Step           Time         Lambda
14                2        2.00000        0.00000"))

> grep("Potential", Lines)   # identify the lines with "Potential"
[1]  4 11

需要转到下一行并获得第5项:

> read.table(text=Lines[ grep("Potential", Lines)+1])[ , 5]
[1] -721252 -743766