[英]R: Generate variable in data.frame using minif on other columns
[英]Append row in data.frame using list of variable with same name of columns - R Language
親愛的!
在一個小例子中總結我的問題......
我想 append 使用與 data.frame 列同名的變量列表在 data.frame 中的一行,如下所示:
#createing a blank data.frame
df <- data.frame(matrix(ncol=3, nrow=0))
#naming the header
head <- c("col1", "col2", "col3")
# assigning header to data.frame
colnames(df) <- head
# creating three variables with the same name of header
col1 <- 1
col2 <- 2
col3 <- 3
#appending the row
rbind(df, list(col1, col2, col3))
代碼運行,但 df 繼續為空白。 我希望 df 得到這樣的結果:
col1 col2 col3
1 2 3
幫我解決這個 rbind。
如果使用names()
function,可以重命名R中的列
#createing a blank data.frame
df <- data.frame(matrix(ncol=3, nrow=0))
#naming the header
head <- c("col1", "col2", "col3")
# assigning header to data.frame
colnames(df) <- head
# creating three variables with the same name of header
col1 <- 1
col2 <- 2
col3 <- 3
#appending the row
df2 <- rbind(df, list(col1, col2, col3))
names(df2) <- c("col1", "col2", "col3")
df2
產生下面的 output
col1 col2 col3
1 2 3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.