我有以下数据框,我想将每个值分配给一个bin,
title
1 MotorolaROID RAZR MAXX 4G Android Phone Black 32GBVerizon Wireless.jpg^HTC EVO 4G 1GB White Sprint Smartphone.jpg
2 MotorolaROID RAZR MAXX 4G Android Phone Black 32GBVerizon Wireless.jpg^NEW 4 0 Android 2 3 Unlocked Quad Bands GPS Bluetooth Wifi Smart Cell phone G10.jpg
3 MotorolaROID RAZR MAXX 4G Android Phone Black 32GBVerizon Wireless.jpg^Motorola Droid X2 Verizon BAD ESN GOOD Condition 100 Functional.jpg
4 MotorolaROID RAZR MAXX 4G Android Phone Black 32GBVerizon Wireless.jpg^UNLOCKED Huawei Ideos S7 Tablet Smartphone.jpg
5 MotorolaROID RAZR MAXX 4G Android Phone Black 32GBVerizon Wireless.jpg^Apple iPhone 4 16GB Black AT&T Smartphone MC318LLA .jpg
6 MotorolaROID RAZR MAXX 4G Android Phone Black 32GBVerizon Wireless.jpg^Apple iPhone 4 16GB Black Factory Unlocked Smartphone.jpg
column1 column2 column3 column4 column5 column6 column7
1 0.978 0.635 0.973 0.7619048 0.6383881 0.8339921 0.06666667
2 0.343 0.702 0.990 0.2623762 0.6150583 0.9285714 0.04166667
3 0.984 0.675 0.712 0.7056277 0.6770944 0.5612648 0.00000000
4 0.798 0.648 0.931 0.4090909 0.5864263 0.8571429 0.00000000
5 0.898 0.709 0.993 0.5000000 0.6951220 0.9328063 0.05882353
6 0.898 0.709 0.993 0.5000000 0.6951220 0.9328063 0.06250000
当我尝试运行以下行时,我收到错误cut.default(newX[, i], ...) : 'x' must be numeric
错误我知道这是因为我的第一列是标题列。如何通过忽略第一列来执行此操作。
df_bin <- apply(df, 2, cut, c(-Inf, seq(0.5, 1, 0.1), Inf), labels=0:7)
答案 0 :(得分:4)
通过-1
计数排除“除第一列以外的所有内容”:
df_bin <- apply(df[,-1], 2, cut, c(-Inf, seq(0.5, 1, 0.1), Inf), labels=0:7)
此处的关键是df[,-1]
与df
。