首页 > 解决方案 > Is there a R function to print & export column names to excel?

问题描述

Im trying to append survey data sets of different versions of the survey to create a master file. But, the different survey versions do not have uniform number of columns across. ( E.g : V1 - 10k vars, V2- 20k vars). I would like to see what are the 10k missing vars in V1 in comparison to V2.

Is there a R function which could print & export the colnames of the data to excel?

Thank you.

标签: rdata-cleaning

解决方案


First, you can make a dataframe of column names:

df1_names <- as.data.frame(colnames(df1))
df2_names <- as.data.frame(colnames(df2))

If you want your column names across rows (wide instead of long), you can transpose them:

df1_names <- as.data.frame(t(df1_names))
df2_names <- as.data.frame(t(df2_names))

Then, using the xlsx package, you can write to Excel:

write.xlsx(df1_names, filename)
write.xlsx(df2_names, filename)

推荐阅读