Reputation: 853
Not sure how to ask this, but i'll give it a try:
I have 20 data.frames (e.g. 2006_1, 2006_2, 2007_1, 2007_2, ...) that I imported from MS Access.
Each data.frame has 10 columns and approximately 3 millions rows.
The first column in each data.frame is named secuityName
, which is a list of stock tickers followed by some tags.
I would like to upcase every row in the SecurityName
column in every one of the 20 data.frames.
The structure follows:
2006_1
> **SecurityName** **...**
> AAPL abcdef **...**
> MSFT abcdef **...**
2006_2
> **SecurityName** **...**
> AAPL abcdef **...**
> MSFT abcdef **...**
I would like each one to look like this:
2006_1
> **SecurityName** **...**
> AAPL ABCDEF **...**
> MSFT ABCDEF **...**
I have a vector named *Raw_data_vector* that has all the data.frame names
Raw_data_vector
> 2006_1
> 2006_2
> 2007_1
> 2007_2
> ...
I have tried to use lapply but can't get it to work:
lapply(1:length(Raw_data_vector), function(x) toupper(get(x)[,1]),Raw_data_vector)
Upvotes: 1
Views: 561
Reputation: 43255
This should get you started. I used data.frames
that have characters that start their names so I don't wind up with any funny business.
oh6_1 <- data.frame(SecurityName=letters[1:20], v=1:20)
oh6_2 <- oh6_1
out <- lapply(ls(pattern = 'oh[0-9]_*'),
function(x) {
df <- get(x)
df[, 1] <- toupper(df[, 1])
return(df)
})
Per the comments:
Depending on the names of your data.frames, you'll need to alter the pattern
argument to ls
. Also, R doesn't change things in place (for the most part) instead it creates a new one.
You are seeing it as output to the console because it isn't assigned to anything! Instead use something like out <- llply(ls(...)...)
. Then inspect out
. It will be a list of the data frames you supplied with their additional column. You can inspect R
objects with ?str
.
Finally, this list of objects notion is a common result when working with many similar (or identical) things. It is easy from here to continue doing whatever process you want, accept instead of calling function(data.frame)
in something like a for loop, you can call lapply(list of data frames, function)
... Clear as mud to be sure.
If you must do the lowercase operation to the existing data.frame, you can use the always scary assign
function:
myfun <- function(X) {
df <- get(x)
df[, 1] <- toupper(df[, 1])
assign(x, df, .GlobalEnv)
return(NULL)
}
lapply(ls(), myfun)
now look at your data.frames.
> oh6_1
SecurityName v
1 A 1
2 B 2
3 C 3
4 D 4
5 E 5
6 F 6
7 G 7
8 H 8
9 I 9
10 J 10
11 K 11
12 L 12
13 M 13
14 N 14
15 O 15
16 P 16
17 Q 17
18 R 18
19 S 19
20 T 20
>
Upvotes: 1