Reputation: 11
I have 2 data frames.
msf
has 2 rows and few hundred columns, each column head being a stock name and consisting all numerical elements.currList
has many rows each of which has a stock name. msf
:-
s1 s2 s3 s4 s5 s6
1 0 1 0 0 1 0
2 0 0 1 0 1 0
currList
:-
1 s2
2 s4
3 s5
4 s7
I want to find the column sum of msf
for each stock in currList
. I tried this:
for curr in 1:nrow(currList){
currSum<- sum(paste("msf$",curr, sep=""))
....
}
It gave an error as:
Error in sum(paste("msf$", curr, sep = "")) :
invalid 'type' (character) of argument
I am new to programming and R and need help to achieve what I want.
Upvotes: 0
Views: 49
Reputation: 721
Your question is bit unclear but assuming my understanding is correct, it can be solved as followings :
data = data.frame(t(sample(50, 10)))
data
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1 36 8 24 47 3 33 44 7 45 21
currlist = data.frame(c("X1", "X5", "X6"))
colnames(currlist) = "stocks"
currlist
stocks
1 X1
2 X5
3 X6
rowSums(data[ ,colnames(data) %in% currlist$stocks])
However all of above is not standard / effective way of storing and processing data. You can go over basic data structure and manipultation techniques for the better understanding.
Upvotes: 1