开发者

Is there a better (i.e vectorised) way to put part of a column name into a row of a data frame in R

I have a data frame in R that has come about from running some stats on the result of a melt/cast operation. I want to add a row into this dataframe containing a Nominal value. That Nominal Value is present in the names for each column

df<-as.data.frame(cbind(x=c(1,2,3,4,5),`Var A_100`=c(5,4,3,2,1),`Var B_5`=c(9,8,7,6,5)))
> df
  x Var A_100 Var B_5
1 1         5       9
2 2         4       8
3 3         3       7
4 4         2       6
5 5         1       5

So, I want to create a new row, that contains '100' in the column Var A_100 and '5' in Var B_5. Currently this is what I'm doing but I'm sure there must be a better, vectorised way to do this.

temp_nom<-NULL
for (l in 1:length(names(df))){
 temp_nom[l]<-strsplit(names(df),"_")[[l]][2]
 }
temp_nom
[1] NA    "100" "5"  
df[6,]<-temp_nom
> df
     x Var A_100 Var B_5
1    1         5       9
2    2         4  开发者_高级运维     8
3    3         3       7
4    4         2       6
5    5         1       5
6 <NA>       100       5
rm(temp_nom)

Typically I'd have 16-24 columns. Any ideas?


You can create temp_nom in two ways (at least):

# strsplit create list so you can sapply on it
sapply(strsplit(names(df),"_"), "[", 2)

# using regular expressions:
sub(".+_|[^_]+", "", names(df))

And for assigment you could convert temp_nom to numeric (in other case it mess with column types)

df[nrow(df)+1,] <- as.numeric(temp_nom)

Of course you can do it in one line:

df[nrow(df)+1,] <- as.numeric(sapply(strsplit(names(df),"_"), "[", 2))
# or
df[nrow(df)+1,] <- as.numeric(sub(".+_|[^_]+", "", names(df)))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜