r - 子集数据框行作为列表返回?

标签 r dataframe vector

我想从数据帧的一行中创建一个字符串向量。没有它的行和列名称。这是我的数据框。

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/

相关文章:

R 表达式变量列表

r - 将列名称传递给 Shiny 数据表中的目标选项

python - Pandas 枢轴与重复项

apache-spark - 在集群 : Initial job has not accepted any resources 上运行 Spark

python - Pandas:从保留其顺序的 2D numpy 数组创建数据框

c++ - 使用 Qt 从 mysql 查询构建二维 vector

r - 使用迭代创建加权邻接矩阵

C++ 线程安全 vector .erase

math - 给定两个旋转角度和一个速度,如何计算 x、y、z 速度?

string - 如何将变量(对象)名称转换为字符串