简体   繁体   English

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

[英]Get value of column as column name in R

I have a df: 我有一个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

I want the values in the store columns to become column names and I want to insert the review values in that column, so the output looks like this: 我希望存储列中的值成为列名,并希望在该列中插入评论值,因此输出如下所示:

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

There are two issues with this problem. 这个问题有两个问题。 First of all, the store names will change a lot in the future, so I can't use code like this: 首先,商店名称在将来会发生很大变化,因此我不能使用如下代码:

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

Secondly, I need the values from the review and review1 column to start from the first row in the A and B column, so to say. 其次,可以说,我需要来自review和review1列的值从A和B列的第一行开始。 For example, column a = book = 0, shirt = 2, magazine = 3. 例如,列a =书= 0,衬衫= 2,杂志= 3。

I'm really stuck on this, any help would be much appreciated! 我真的很坚持,任何帮助将不胜感激!

Reproducible code: 可复制的代码:

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))

It is difficult to develop a universal method for this because it is unclear whether the number of columns will be the same, or can they be matched one-to-one all the time (say, for every storeN there is reviewerN ) etc. Here's the code that does what you wanted, but I am not sure if it fits the purpose. 为此,很难开发出通用的方法,因为尚不清楚列数是否相同,或者是否始终可以将它们一对一地匹配(例如,对于每个storeN都有reviewerN )等。该代码可以满足您的要求,但是我不确定它是否符合目的。 You should have explained your problem more thoroughly. 您应该更彻底地解释您的问题。

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

As for your second problem, it is a mad task because it renders your data as very, very messy. 至于第二个问题,这是一个疯狂的任务,因为它使您的数据非常混乱。 I mean, how do you know that book = 3, not shirt ? 我的意思是,您怎么知道那book = 3,而不是shirt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM