crazysantaclaus
crazysantaclaus

Reputation: 633

R add columns based on modifying other columns of dataframes within a list

I would like to add a new column D to data.frames in a list that contains the first part of column B. But I'm not sure how to adress within lists down to the column level?

create some data

df1 <- data.frame(A = "hey", B = "wass.7", C = "up")
df2 <- data.frame(A = "how", B = "are.1", C = "you")
dfList <- list(df1,df2)

desired output:

# a new column removing the last part of column B

[[1]]
    A      B  C D
1 hey wass.7 up wass

[[2]]
    A     B   C D
1 how are.1 you are

for each data frame I did this, which worked

df1$D<-sub('\\..*', '', df1$B)

in a function I tried this, which is probably not correctly addressing the columns and returns "unexpected symbol..."

dfList <- lapply(rapply(dfList, function(x) 
x$D<-sub('\\..*', '', x$B) how = "list"),
  as.data.frame)

the lapply(rapply) part is copied from Using gsub in list of dataframes with R

Upvotes: 1

Views: 61

Answers (2)

Parfait
Parfait

Reputation: 107767

The rapply solution does work. However, you needed a comma before the how argument to resolve the error. Additionally, you will NOT be able to assign one new column only replace existing ones. Since rapply is a recursive call, it will run the gsub across every element in nested list so across ALL columns of ALL dataframes.

Otherwise use a simple lapply per @JilberUrbina's answer.

df1 <- data.frame(A = "hey", B = "wass.7", C = "up", stringsAsFactors = F)
df2 <- data.frame(A = "how", B = "are.1", C = "you", stringsAsFactors = F)
dfList <- list(df1,df2)

dfList <- lapply(rapply(dfList, function(x) 
  sub('\\..*', '', x), how = "list"),
  as.data.frame)

dfList

# [[1]]
#     A    B  C
# 1 hey wass up

# [[2]]
#     A   B   C
# 1 how are you

Upvotes: 1

Jilber Urbina
Jilber Urbina

Reputation: 61214

Check this out

lapply(dfList, function(x){
   x$D <-sub('\\..*', '', x$B);
   x
 })
[[1]]
    A      B  C    D
1 hey wass.7 up wass

[[2]]
    A     B   C   D
1 how are.1 you are

Upvotes: 2

Related Questions