rename column in dataframe using variable name R -
i have number of data frames. each same format. this:
b c 1 -0.02299388 0.71404158 0.8492423 2 -1.43027866 -1.96420767 -1.2886368 3 -1.01827712 -0.94141194 -2.0234436
i change name of third column--c--so includes part if name of variable name associated data frame.
for variable df_elephant
data frame should this:
b c.elephant 1 -0.02299388 0.71404158 0.8492423 2 -1.43027866 -1.96420767 -1.2886368 3 -1.01827712 -0.94141194 -2.0234436
i have function change column name:
rename_columns <- function(x) { colnames(x)[colnames(x)=='c'] <- paste( 'c', strsplit (deparse (substitute(x)), '_')[[1]][2], sep='.' ) return(x) }
this works data frames. however, provide list of data frames not have call function multiple times hand. if use lapply
so:
lapply( list (df_elephant, df_horse), rename_columns )
the function renames data frames na
rather portion of variable name.
[[1]] b c.na 1 -0.02299388 0.71404158 0.8492423 2 -1.43027866 -1.96420767 -1.2886368 3 -1.01827712 -0.94141194 -2.02344361 [[2]] b c.na 1 0.45387054 0.02279488 1.6746280 2 -1.47271378 0.68660595 -0.2505752 3 1.26475917 -1.51739927 -1.3050531
is there way kind provide list of data frames function , produce desired result?
you trying process data frame column names instead of actual lists' name. , why it's not working.
# generating random data n = 3 item1 = data.frame(a = runif(n), b = runif(n), c = runif(n)) item2 = data.frame(a = runif(n), b = runif(n), c = runif(n)) mylist = list(df_elephant = item1, df_horse = item2) # 1- why code doesnt work: --------------- names(mylist) # return actual names want use : [1] "df_elephant" "df_horse" lapply(mylist, names) # return dataframes' column names. , thats why getting "na" # 2- how make work: --------------- lapply(seq_along(mylist), # return array of indicies function(i){ dfname = names(mylist)[i] # list name dfname.animal = unlist(strsplit(dfname, "_"))[2] # split on underscore , take second element df = mylist[[i]] # copy actual data frame colnames(df)[colnames(df) == "c"] = paste("c", dfname.animal, sep = ".") # change column names return(df) # return new df }) # [[1]] # b c.elephant # 1 0.8289368 0.06589051 0.2929881 # 2 0.2362753 0.55689663 0.4854670 # 3 0.7264990 0.68069346 0.2940342 # # [[2]] # b c.horse # 1 0.08032856 0.4137106 0.6378605 # 2 0.35671556 0.8112511 0.4321704 # 3 0.07306260 0.6850093 0.2510791
Comments
Post a Comment