参考不同列中的值

时间:2013-11-23 00:50:25

标签: r if-statement dataframe

这个问题最好通过例子询问。我在R中有以下数据框:

  date  SPX.Index.1stM.100..MnySPXindexprice day    c
1   2005-01-03  12.00050    1202.084    Monday          1202.084
2   2005-01-04  11.90630    1188.045    Tuesday         1188.045
3   2005-01-05  12.03420    1183.737    Wednesday   1183.737
4   2005-01-06  11.52000    1187.885    Thursday    1187.885
5   2005-01-07  11.15550    1186.193    Friday          1190.000
6   2005-01-08  11.08770    1190.245    Saturday    1190.245
7   2005-01-09  11.08770    1190.245    Sunday      1190.245
8   2005-01-10  11.08770    1190.245    Monday          1190.245

如果当天是“星期五”,我想在星期五的同一'时段'中返回星期一的SPXindexprice VALUE。所以在上面的例子中,在第5行中,我有一个值为1202.084的新列(日期列中的星期一)希望这有意义吗?

2 个答案:

答案 0 :(得分:0)

这是Python的解决方案:

df = open("dataframe.txt").read()
splitData = df.split("\n")
out = open("output.txt", "w")

for row in splitData:
  splitRow = row.split()
  if len(splitRow) >= 4:
    if splitRow[4] == "Monday":
      mondayValue = splitRow[3]

      out.write(str(splitRow[0]) + "\t"
      + str(splitRow[1]) + "\t"
      + str(splitRow[2]) + "\t"
      + str(splitRow[3]) + "\t"
      + str(splitRow[4]) + "\t"
      + str(splitRow[5]) + "\n")

    elif splitRow[4] == "Friday":
      out.write(str(splitRow[0]) + "\t"
      + str(splitRow[1]) + "\t"
      + str(splitRow[2]) + "\t"
      + str(mondayValue) + "\t"
      + str(splitRow[4]) + "\t"
      + str(mondayValue) + "\n")

    else:
      out.write(str(splitRow[0]) + "\t"
      + str(splitRow[1]) + "\t"
      + str(splitRow[2]) + "\t"
      + str(splitRow[3]) + "\t"
      + str(splitRow[4]) + "\t"
      + str(splitRow[5]) + "\n")

输出以制表符分隔:

row date    SPX.Index   1stM.100    MnySPXindexprice    day
1   2005-01-03  12.0005 1202.084    Monday  1202.084
2   2005-01-04  11.9063 1188.045    Tuesday 1188.045
3   2005-01-05  12.0342 1183.737    Wednesday   1183.737
4   2005-01-06  11.52   1187.885    Thursday    1187.885
5   2005-01-07  11.1555 1202.084    Friday  1202.084
6   2005-01-08  11.0877 1190.245    Saturday    1190.245
7   2005-01-09  11.0877 1190.245    Sunday  1190.245
8   2005-01-10  11.0877 1190.245    Monday  1190.245

答案 1 :(得分:0)

如果您的数据框按日期排序,那么您可以尝试这样做:

#reproducible dataframe
df <- read.table(text="date  SPX.Index MnySPXindexprice day    c
                 2005-01-03  12.00050    1202.084    Monday          1202.084
                 2005-01-04  11.90630    1188.045    Tuesday         1188.045
                 2005-01-05  12.03420    1183.737    Wednesday   1183.737
                 2005-01-06  11.52000    1187.885    Thursday    1187.885
                 2005-01-07  11.15550    1186.193    Friday          1190.000
                 2005-01-08  11.08770    1190.245    Saturday    1190.245
                 2005-01-09  11.08770    1190.245    Sunday      1190.245
                 2005-01-10  11.08770    1190.245    Monday          1190.245",
                 header=TRUE)

#assign monday row
df$MondayValue <- 
  ifelse(df$day!="Friday", NA,
         df[as.numeric(rownames(df[ df$day=="Friday",]))-4,"MnySPXindexprice"])