获取列的值作为R中的列名

时间:2017-10-22 22:22:54

标签: r

我有一个df:

  product   store   store1  review   review1 
  book      A       B                 
  shirt     A       B                 
  pen       A       B       
  cd        A       B        0         2
  dress     A       B        2         1
  magazine  A       B        3         1

我希望store列中的值成为列名,我想在该列中插入审阅值,因此输出如下所示:

  product   A       B 
  book      0       2
  shirt     2       1
  pen       3       1

此问题存在两个问题。首先,商店名称将来会发生很大变化,所以我不能使用这样的代码:

 names(newdf)[names(newdf) == 'store'] <- 'a'

其次,我需要来自review和review1列的值从A和B列的第一行开始,所以说。例如,列a = book = 0,shirt = 2,magazine = 3.

我真的坚持这一点,任何帮助都会非常感激!

可重复的代码:

df <- data.frame(product = c("book","shirt", "pen", "cd", "dress", "magazine"), store=c("A", "A", "A", "A", "A", "A"),
                 store1=c("B", "B", "B", "B", "B", "B"), review=c("", "", "", 0, 2, 3), review1 =c("", "", "", 2, 1, 1))

1 个答案:

答案 0 :(得分:1)

很难为此开发一种通用的方法,因为不清楚列的数量是否相同,或者它们是否可以一直一对一地匹配(例如,对于每个storeNreviewerN)等。这是代码,你做了你想要的,但我不确定它是否符合目的。你应该更彻底地解释你的问题。

df <- data.frame(product = c("book","shirt", "pen", "cd", "dress", "magazine"), store=c("A", "A", "A", "A", "A", "A"),
                 store1=c("B", "B", "B", "B", "B", "B"), review=c("", "", "", 0, 2, 3), review1 =c("", "", "", 2, 1, 1))


# Convert factors to character
df <- data.frame(lapply(df, as.character), stringsAsFactors=FALSE)

# Blanks to NAs
df[df==""] <- NA

# Indicate which columns contain values to rename other columns
cols_with_values <- c(2,3)

# save first row in these columns
new_columns_names <- as.character(df[1,cols_with_values])

# Kill them!
df[,cols_with_values] <- NULL

# Rename columns
names(df) <- c(names(df[1]), new_columns_names)

# Show rows without NAs
df[complete.cases(df), ]
   product A B
4       cd 0 2
5    dress 2 1
6 magazine 3 1

至于你的第二个问题,这是一项疯狂的任务,因为它会使你的数据非常非常混乱。我的意思是,你怎么知道book = 3,而不是shirt