R with Lookup Table
-
14-07-2021 - |
题
I have two files which contain time series in this format:
Tag, T1, T2, ... Tn
Stock-1, 2, 3, .. 6
Stock-2, 3, 2, .. 7
.
.
Stock-m, 2, 1, .. 9
In R, for any tag (ex. Stock-x) I want to take related arrays from both files and calculate cross correlation(ccf) for them. How I can do that ?
Note: If it would be easier, I can change the file format.
Update:
I read from file into data.frames with this:
file1 = read.table("file1.txt", header = TRUE, sep = ",")
How will I reach to specific rows with Tag name such as "Stock-5" ?
解决方案
I removed the "Tag" from the files:
T1, T2, ... Tn
Stock-1, 2, 3, .. 6
Stock-2, 3, 2, .. 7
.
.
Stock-m, 2, 1, .. 9
Then I was able get results with this R code:
file1 = read.table("file1.txt",head=TRUE,sep=",")
file2 = read.table("file2.txt",head=TRUE,sep=",")
row1 = file1["Stock-5",]
row2 = file2["Stock-5",]
vec1 = c(t(row1))
vec2 = c(t(row2))
ccf(vec1,vec2)
其他提示
It seems that the question might be answered with:
f1stock5df <- subset(file1,Tag %in% c("stock5"))
f2stock5df <- subset(file2,Tag %in% c("stock5"))
bothstocks <- rbind(f1stock5df, f2stock5df)
with(bothstocks, ccf(x,y))
不隶属于 StackOverflow