我想从数据帧的一行中创建一个字符串向量。没有它的行和列名称。这是我的数据框。
agreement = c("Strongly Disagree"," Disagree", "Neither", "Agree", "Strongly Agree")
likelihood = c("Very unlikely","Unlikely", "Neither", "Likely", "Very Likely")
df <- as.data.frame(rbind(agreement, likelihood))
获得数据框后,如何返回没有列值的字符向量?例如
> "Strongly Disagree"," Disagree", "Neither", "Agree", "Strongly Agree"
我尝试过 as.character
,但我得到的返回是数值。
as.character(df[1,1:5])
> "1" "1" "1" "1" "1"
我也尝试过 as.vector
,但它返回一个包含列名称的列表。
as.vector(df[1,1:5])
> V1 V2 V3 V4 V5
agreement Strongly Disagree Disagree Neither Agree Strongly Agree
如有任何帮助,我们将不胜感激!
最佳答案
创建数据帧时,您需要将stringsAsFactors
设置为FALSE
。
df <- as.data.frame(rbind(agreement, likelihood), stringsAsFactors = FALSE)
as.character(df[1,1:5])
现在结果为
"Strongly Disagree" " Disagree" "Neither" "Agree" "Strongly Agree"
检索所有行或列时,您只需将该字段留空即可,例如(df[1,]
返回包含所有列的第一行)
关于r - 子集数据框行作为列表返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49432614/