在r中创建一系列值标签

时间:2018-02-12 17:13:12

标签: r label

我的数据如下:

<CustomPhoneStateListener.java>

public CustomPhoneStateListener(Context context) {
    mContext = context;
}

@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
    super.onSignalStrengthsChanged(signalStrength);
    Log.i(LOG_TAG, "onSignalStrengthsChanged: " + signalStrength);
    if (signalStrength.isGsm()) {
        Log.i(LOG_TAG, "onSignalStrengthsChanged: getGsmBitErrorRate "
                + signalStrength.getGsmBitErrorRate());
        Log.i(LOG_TAG, "onSignalStrengthsChanged: getGsmSignalStrength "
                + signalStrength.getGsmSignalStrength());
    } else if (signalStrength.getCdmaDbm() > 0) {
        Log.i(LOG_TAG, "onSignalStrengthsChanged: getCdmaDbm "
                + signalStrength.getCdmaDbm());
        Log.i(LOG_TAG, "onSignalStrengthsChanged: getCdmaEcio "
                + signalStrength.getCdmaEcio());
    } else {
        Log.i(LOG_TAG, "onSignalStrengthsChanged: getEvdoDbm "
                + signalStrength.getEvdoDbm());
        Log.i(LOG_TAG, "onSignalStrengthsChanged: getEvdoEcio "
                + signalStrength.getEvdoEcio());
        Log.i(LOG_TAG, "onSignalStrengthsChanged: getEvdoSnr "
                + signalStrength.getEvdoSnr());
    }

          try {
        Method[] methods = android.telephony.SignalStrength.class
                .getMethods();
        for (Method mthd : methods) {
            if (mthd.getName().equals("getLteSignalStrength")
                    || mthd.getName().equals("getLteRsrp")
                    || mthd.getName().equals("getLteRsrq")
                    || mthd.getName().equals("getLteRssnr")
                    || mthd.getName().equals("getLteCqi")) {
                Log.i(LOG_TAG,
                        "onSignalStrengthsChanged: " + mthd.getName() + " "
                                + mthd.invoke(signalStrength));
            }
        }
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
  e.printStackTrace();
    }
}

如何自动为区域创建标签,使其看起来像这样?

               outc_n state region
largezip1_outc     96    FL      1
largezip2_outc     74    FL      2
largezip3_outc    251    FL      3
largezip4_outc    176    FL      4
largezip5_outc    161    FL      5
largezip6_outc     95    FL      6

谢谢!

3 个答案:

答案 0 :(得分:2)

library(dplyr)

dat <- 
read.table(text = "               outc_n state region
largezip1_outc     96    FL      1
largezip2_outc     74    FL      2
largezip3_outc    251    FL      3
largezip4_outc    176    FL      4
largezip5_outc    161    FL      5
largezip6_outc     95    FL      6")

dat %>%
  mutate(region = paste(state, "L", region, sep = "-"))

您可以通过一些小编辑以编程方式更改L语句中代替paste()的内容。

答案 1 :(得分:1)

假设您要添加L,因为行名的第一个字母是L,我们可以使用paste来连接不同的字符串。

dat$region <- with(dat, paste(state,
                              toupper(substring(rownames(dat), 1, 1)),
                              region,
                              sep = "-"))
dat
#                outc_n state region
# largezip1_outc     96    FL FL-L-1
# largezip2_outc     74    FL FL-L-2
# largezip3_outc    251    FL FL-L-3
# largezip4_outc    176    FL FL-L-4
# largezip5_outc    161    FL FL-L-5
# largezip6_outc     95    FL FL-L-6

数据

dat <- read.table(text = "               outc_n state region
largezip1_outc     96    FL      1
                  largezip2_outc     74    FL      2
                  largezip3_outc    251    FL      3
                  largezip4_outc    176    FL      4
                  largezip5_outc    161    FL      5
                  largezip6_outc     95    FL      6",
                  header = TRUE, stringsAsFactors = FALSE)

答案 2 :(得分:1)

使用data.table

 df <- read.table(header=T, text="large outc_n state region
                       largezip1_outc     96    FL      1
                       largezip2_outc     74    FL      2
                       largezip3_outc    251    FL      3
                       largezip4_outc    176    FL      4
                       largezip5_outc    161    FL      5
                       largezip6_outc     95    FL      6")

library(data.table)
setDT(df)
## using paste0 to paste different values of columns desired and getting 
## first letter of first column and converting it to upper case as desired
df[, region := paste0(state, "-", toupper(substr(large, 1, 1)), "-", region)]


           large   outc_n state region
1: largezip1_outc     96    FL FL-L-1
2: largezip2_outc     74    FL FL-L-2
3: largezip3_outc    251    FL FL-L-3
4: largezip4_outc    176    FL FL-L-4
5: largezip5_outc    161    FL FL-L-5
6: largezip6_outc     95    FL FL-L-6